eaglelandsonce commited on
Commit
444f78b
·
verified ·
1 Parent(s): c5d8a1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -49
app.py CHANGED
@@ -1,50 +1,57 @@
1
  import streamlit as st
2
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
- from huggingface_hub import snapshot_download, login
4
- from pathlib import Path
5
-
6
- def main():
7
- st.title("Codestral Inference with Hugging Face")
8
-
9
- # Get the Hugging Face API token from the user
10
- hf_token = st.text_input("Enter your Hugging Face API token", type="password")
11
- if not hf_token:
12
- st.warning("Please enter your Hugging Face API token to proceed.")
13
- st.stop()
14
-
15
- # Login to Hugging Face Hub
16
- login(hf_token)
17
-
18
- # Download the model files
19
- st.text("Downloading model...")
20
- model_id = "mistralai/Codestral-22B-v0.1"
21
- local_model_path = Path.home().joinpath('mistral_models', model_id)
22
- local_model_path.mkdir(parents=True, exist_ok=True)
23
-
24
- snapshot_download(repo_id=model_id, local_dir=local_model_path, use_auth_token=hf_token)
25
- st.success("Model downloaded successfully!")
26
-
27
- # Load the model and tokenizer
28
- st.text("Loading model...")
29
- tokenizer = AutoTokenizer.from_pretrained(local_model_path, use_auth_token=hf_token)
30
- model = AutoModelForCausalLM.from_pretrained(local_model_path, use_auth_token=hf_token)
31
- generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
32
- st.success("Model loaded successfully!")
33
-
34
- user_input = st.text_area("Enter your instruction", "Explain Machine Learning to me in a nutshell.")
35
- max_tokens = st.slider("Max Tokens", min_value=10, max_value=500, value=64)
36
- temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.7)
37
-
38
- if st.button("Generate"):
39
- with st.spinner("Generating response..."):
40
- result = generate_response(generator, user_input, max_tokens, temperature)
41
- st.success("Response generated!")
42
- st.text_area("Generated Response", result, height=200)
43
-
44
- def generate_response(generator, user_input, max_tokens, temperature):
45
- response = generator(user_input, max_new_tokens=max_tokens, do_sample=True, temperature=temperature)
46
- result = response[0]['generated_text']
47
- return result
48
-
49
- if __name__ == "__main__":
50
- main()
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
+
4
+ """
5
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
+ """
7
+ client = InferenceClient(model_id="mistralai/Codestral-22B-v0.1", token="your_token_here")
8
+
9
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
10
+ messages = [{"role": "system", "content": system_message}]
11
+ for user_msg, assistant_msg in history:
12
+ if user_msg:
13
+ messages.append({"role": "user", "content": user_msg})
14
+ if assistant_msg:
15
+ messages.append({"role": "assistant", "content": assistant_msg})
16
+ messages.append({"role": "user", "content": message})
17
+
18
+ response = ""
19
+ try:
20
+ for message in client.chat_completion(
21
+ messages=messages,
22
+ max_tokens=max_tokens,
23
+ stream=True,
24
+ temperature=temperature,
25
+ top_p=top_p,
26
+ ):
27
+ token = message.choices[0].delta.content
28
+ response += token
29
+ yield response
30
+ except Exception as e:
31
+ yield f"Error: {e}"
32
+
33
+ # Streamlit interface
34
+ st.title("Chat with Codestral Model")
35
+ system_message = st.text_input("System message", value="You are an expert python coder with in depth knowledge of langchain.")
36
+ max_tokens = st.slider("Max new tokens", min_value=1, max_value=2048, value=2048, step=1)
37
+ temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.6, step=0.1)
38
+ top_p = st.slider("Top-p (nucleus sampling)", min_value=0.1, max_value=1.0, value=0.95, step=0.05)
39
+
40
+ history = []
41
+
42
+ if "history" not in st.session_state:
43
+ st.session_state.history = []
44
+
45
+ def get_response():
46
+ user_input = st.session_state.user_input
47
+ if user_input:
48
+ st.session_state.history.append((user_input, ""))
49
+ response_generator = respond(user_input, st.session_state.history, system_message, max_tokens, temperature, top_p)
50
+ response = ""
51
+ for r in response_generator:
52
+ response = r
53
+ st.session_state.history[-1] = (user_input, response)
54
+
55
+ st.text_area("Chat History", value="\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in st.session_state.history]), height=300)
56
+
57
+ st.text_input("Your message:", key="user_input", on_change=get_response)