JairoCesar commited on
Commit
21a807d
·
verified ·
1 Parent(s): 2fe2b2a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from huggingface_hub import InferenceClient
3
+
4
+ # Initialize the client
5
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
6
+
7
+ # Function to format the prompt
8
+ def format_prompt(message, history):
9
+ prompt = "<s>"
10
+ for user_prompt, bot_response in history:
11
+ prompt += f"[INST] {user_prompt} [/INST]"
12
+ prompt += f" {bot_response} "
13
+ prompt += f"[INST] {message} [/INST]"
14
+ return prompt
15
+
16
+ # Function to generate response
17
+ def generate(prompt, history, temperature=0.3, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
18
+ temperature = max(float(temperature), 1e-2)
19
+ top_p = float(top_p)
20
+
21
+ generate_kwargs = dict(
22
+ temperature=temperature,
23
+ max_new_tokens=max_new_tokens,
24
+ top_p=top_p,
25
+ repetition_penalty=repetition_penalty,
26
+ do_sample=True,
27
+ seed=42,
28
+ )
29
+
30
+ formatted_prompt = format_prompt(prompt, history)
31
+
32
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
33
+ output = ""
34
+ for response in stream:
35
+ output += response.token.text
36
+ return output
37
+
38
+ # Streamlit interface
39
+ st.title("Preguntale al Buho")
40
+
41
+ # Chat history
42
+ if 'history' not in st.session_state:
43
+ st.session_state.history = []
44
+
45
+ # User input
46
+ Entrada_usuario = st.text_input("Tu duda:", key="Entrada_usuario")
47
+
48
+ # Generate response and update history
49
+ if st.button("Enviar"):
50
+ if Entrada_usuario:
51
+ bot_response = generate(Entrada_usuario, st.session_state.history)
52
+ st.session_state.history.append((Entrada_usuario, bot_response))
53
+ # st.session_state.user_input = ""
54
+
55
+ # Display conversation
56
+ chat_text = ""
57
+ for user_msg, bot_msg in st.session_state.history:
58
+ chat_text += f"Tu: {user_msg}\nBuhIA: {bot_msg}\n\n"
59
+ st.text_area("La respuesta", value=chat_text, height=300, disabled=False)