Chad commited on
Commit ·
f190d31
1
Parent(s): 2291d9c
resetwdwdwdwdwwdwdwd
Browse files
app.py
CHANGED
|
@@ -9,12 +9,13 @@ if not api_key:
|
|
| 9 |
|
| 10 |
client = Groq(api_key=api_key)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
if chat_history is None:
|
| 14 |
chat_history = []
|
| 15 |
|
| 16 |
if isinstance(chat_history, list):
|
| 17 |
-
chat_history = [
|
|
|
|
| 18 |
|
| 19 |
conversation = [{"role": "system", "content": (
|
| 20 |
"Determine the language of the user's question and respond in the same language. "
|
|
@@ -54,11 +55,8 @@ def respond(user_input, chat_history):
|
|
| 54 |
|
| 55 |
with gr.Blocks() as interface:
|
| 56 |
gr.Markdown("## History Chatbot")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
chatbot = gr.Chatbot(type="messages")
|
| 60 |
-
state = gr.State([]) # Store conversation history
|
| 61 |
-
|
| 62 |
user_input = gr.Textbox(placeholder="Enter your question here...")
|
| 63 |
send_button = gr.Button("Send")
|
| 64 |
|
|
@@ -77,19 +75,13 @@ with gr.Blocks() as interface:
|
|
| 77 |
"Qui était la première personne à marcher sur la lune?"
|
| 78 |
]
|
| 79 |
|
| 80 |
-
# Ensure clicking an example sends the message
|
| 81 |
-
def process_example(example):
|
| 82 |
-
return example, state
|
| 83 |
-
|
| 84 |
gr.Examples(
|
| 85 |
examples=examples,
|
| 86 |
-
inputs=user_input
|
| 87 |
-
outputs=[user_input, state], # Set input and preserve history
|
| 88 |
-
fn=process_example
|
| 89 |
)
|
| 90 |
|
| 91 |
send_button.click(
|
| 92 |
-
fn=
|
| 93 |
inputs=[user_input, state],
|
| 94 |
outputs=[chatbot, state, user_input]
|
| 95 |
)
|
|
@@ -98,4 +90,4 @@ with gr.Blocks() as interface:
|
|
| 98 |
reset_button.click(lambda: [], outputs=[state])
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
| 101 |
-
interface.launch()
|
|
|
|
| 9 |
|
| 10 |
client = Groq(api_key=api_key)
|
| 11 |
|
| 12 |
+
def generate_reply(user_input, chat_history):
|
| 13 |
if chat_history is None:
|
| 14 |
chat_history = []
|
| 15 |
|
| 16 |
if isinstance(chat_history, list):
|
| 17 |
+
chat_history = [(str(msg[0]), str(msg[1])) for msg in chat_history if isinstance(msg, (list, tuple)) and len(msg) == 2]
|
| 18 |
+
|
| 19 |
|
| 20 |
conversation = [{"role": "system", "content": (
|
| 21 |
"Determine the language of the user's question and respond in the same language. "
|
|
|
|
| 55 |
|
| 56 |
with gr.Blocks() as interface:
|
| 57 |
gr.Markdown("## History Chatbot")
|
| 58 |
+
chatbot = gr.Chatbot(type="messages")
|
| 59 |
+
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
| 60 |
user_input = gr.Textbox(placeholder="Enter your question here...")
|
| 61 |
send_button = gr.Button("Send")
|
| 62 |
|
|
|
|
| 75 |
"Qui était la première personne à marcher sur la lune?"
|
| 76 |
]
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
gr.Examples(
|
| 79 |
examples=examples,
|
| 80 |
+
inputs=user_input
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
send_button.click(
|
| 84 |
+
fn=generate_reply,
|
| 85 |
inputs=[user_input, state],
|
| 86 |
outputs=[chatbot, state, user_input]
|
| 87 |
)
|
|
|
|
| 90 |
reset_button.click(lambda: [], outputs=[state])
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
+
interface.launch(share=True)
|
test.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from groq import Groq
|
| 4 |
-
|
| 5 |
-
api_key = os.getenv("GROQ_API_KEY")
|
| 6 |
-
|
| 7 |
-
if not api_key:
|
| 8 |
-
raise ValueError("GROQ_API_KEY is not set. Please set it as an environment variable.")
|
| 9 |
-
|
| 10 |
-
client = Groq(api_key=api_key)
|
| 11 |
-
|
| 12 |
-
def generate_reply(user_input, chat_history):
|
| 13 |
-
if chat_history is None:
|
| 14 |
-
chat_history = []
|
| 15 |
-
|
| 16 |
-
if isinstance(chat_history, list):
|
| 17 |
-
chat_history = [(str(msg[0]), str(msg[1])) for msg in chat_history if isinstance(msg, (list, tuple)) and len(msg) == 2]
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
conversation = [{"role": "system", "content": (
|
| 21 |
-
"Determine the language of the user's question and respond in the same language. "
|
| 22 |
-
"Users may switch between English and French, so maintain consistency. "
|
| 23 |
-
"Only answer history-related questions. If asked about another topic, reply with: "
|
| 24 |
-
"'I am a history chatbot and can only answer history-related questions.' "
|
| 25 |
-
"(or the equivalent in French, depending on the input language). Then stop responding."
|
| 26 |
-
)}]
|
| 27 |
-
|
| 28 |
-
for entry in chat_history:
|
| 29 |
-
if entry["role"] == "user":
|
| 30 |
-
conversation.append({"role": "user", "content": entry["content"]})
|
| 31 |
-
elif entry["role"] == "assistant":
|
| 32 |
-
conversation.append({"role": "assistant", "content": entry["content"]})
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
conversation.append({"role": "user", "content": user_input})
|
| 36 |
-
|
| 37 |
-
try:
|
| 38 |
-
response = client.chat.completions.create(
|
| 39 |
-
model="llama3-70b-8192",
|
| 40 |
-
messages=conversation,
|
| 41 |
-
temperature=0.7,
|
| 42 |
-
max_completion_tokens=512,
|
| 43 |
-
top_p=0.95,
|
| 44 |
-
stream=True,
|
| 45 |
-
stop=None,
|
| 46 |
-
)
|
| 47 |
-
reply = response.choices[0].message.content or "..."
|
| 48 |
-
except Exception as error:
|
| 49 |
-
reply = f"Error: {error}"
|
| 50 |
-
|
| 51 |
-
chat_history.append({"role": "user", "content": user_input})
|
| 52 |
-
chat_history.append({"role": "assistant", "content": reply})
|
| 53 |
-
|
| 54 |
-
return chat_history, chat_history, ""
|
| 55 |
-
|
| 56 |
-
with gr.Blocks() as interface:
|
| 57 |
-
gr.Markdown("## History Chatbot")
|
| 58 |
-
chatbot = gr.Chatbot(type="messages")
|
| 59 |
-
state = gr.State([])
|
| 60 |
-
user_input = gr.Textbox(placeholder="Enter your question here...")
|
| 61 |
-
send_button = gr.Button("Send")
|
| 62 |
-
|
| 63 |
-
examples = [
|
| 64 |
-
"How did the Leaning Tower of Pisa start leaning?",
|
| 65 |
-
"Comment la tour de Pise a-t-elle commencé à pencher ?",
|
| 66 |
-
"Who built the Colosseum?",
|
| 67 |
-
"Qui a construit le Colisée ?",
|
| 68 |
-
"What caused the fall of the Roman Empire?",
|
| 69 |
-
"Quelles sont les causes de la chute de l'Empire romain ?",
|
| 70 |
-
"When did World War II begin and end?",
|
| 71 |
-
"Quand la Seconde Guerre mondiale a-t-elle commencé et terminé ?",
|
| 72 |
-
"What was the significance of the Magna Carta?",
|
| 73 |
-
"Quelle est l'importance de la Magna Carta ?",
|
| 74 |
-
"Who was the first person to step on the moon?",
|
| 75 |
-
"Qui était la première personne à marcher sur la lune?"
|
| 76 |
-
]
|
| 77 |
-
|
| 78 |
-
gr.Examples(
|
| 79 |
-
examples=examples,
|
| 80 |
-
inputs=user_input
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
send_button.click(
|
| 84 |
-
fn=generate_reply,
|
| 85 |
-
inputs=[user_input, state],
|
| 86 |
-
outputs=[chatbot, state, user_input]
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
reset_button = gr.Button("New Conversation")
|
| 90 |
-
reset_button.click(lambda: [], outputs=[state])
|
| 91 |
-
|
| 92 |
-
if __name__ == "__main__":
|
| 93 |
-
interface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|