Arjun Singh
commited on
Commit
·
fb4b52f
1
Parent(s):
a363b5a
Added streaming and conversation history
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
import os
|
|
@@ -8,18 +9,30 @@ You are a helpful, respectful and honest assistant. Always answer as helpfully a
|
|
| 8 |
groq_api_key = os.environ.get("groq_api_key")
|
| 9 |
|
| 10 |
client = Groq(
|
| 11 |
-
api_key=groq_api_key
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
| 14 |
def chat_with_llama(prompt):
|
|
|
|
|
|
|
| 15 |
chat_completion = client.chat.completions.create(
|
| 16 |
-
messages=
|
| 17 |
model="llama-3.1-8b-instant",
|
| 18 |
)
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def clear_fields():
|
|
|
|
|
|
|
| 23 |
return "", "" # Clear prompt and output, but not the API key
|
| 24 |
|
| 25 |
|
|
@@ -29,17 +42,23 @@ with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
|
|
| 29 |
gr.Markdown("<center><h2>Arjun's Chatbot</h2></center>")
|
| 30 |
gr.Markdown("Hi there! I'm an AI assistant tasked with answering one question at a time. I'm instructed to provide complete and truthful responses, without any harmful or biased content. I have some memory issues which are work in progress, so I cannot remember previous conversations, yet!. Happy Chatting!")
|
| 31 |
prompt = gr.Textbox(label='Question', lines=2, max_lines=5, placeholder = 'Type your question here.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
with gr.Group():
|
| 34 |
with gr.Row():
|
| 35 |
submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
|
| 36 |
clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
|
| 37 |
-
|
| 38 |
-
with gr.Row() as output:
|
| 39 |
-
llm_output = gr.Markdown("<center><h3>AI Response</h3></center>")
|
| 40 |
|
| 41 |
-
submit_btn.click(fn=
|
| 42 |
-
clear_btn.click(fn=clear_fields,outputs=[prompt,
|
| 43 |
|
| 44 |
demo.launch()
|
| 45 |
#test comment
|
|
|
|
| 1 |
+
import time
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
import os
|
|
|
|
| 9 |
groq_api_key = os.environ.get("groq_api_key")
|
| 10 |
|
| 11 |
client = Groq(
|
| 12 |
+
api_key=groq_api_key
|
| 13 |
)
|
| 14 |
|
| 15 |
+
conversation_history = [{"role": "system", "content": system_prompt}]
|
| 16 |
+
|
| 17 |
def chat_with_llama(prompt):
|
| 18 |
+
global conversation_history
|
| 19 |
+
conversation_history.append({"role": "user", "content": prompt})
|
| 20 |
chat_completion = client.chat.completions.create(
|
| 21 |
+
messages=conversation_history,
|
| 22 |
model="llama-3.1-8b-instant",
|
| 23 |
)
|
| 24 |
+
response = chat_completion.choices[0].message.content
|
| 25 |
+
conversation_history.append({"role": "assistant", "content": response})
|
| 26 |
+
# Simulate streaming effect
|
| 27 |
+
streamed_response = ""
|
| 28 |
+
for i in range(1, len(response) + 1):
|
| 29 |
+
streamed_response = response[:i]
|
| 30 |
+
yield streamed_response
|
| 31 |
+
time.sleep(0.01) # Adjust the sleep time to control the streaming speed
|
| 32 |
|
| 33 |
def clear_fields():
|
| 34 |
+
global conversation_history
|
| 35 |
+
conversation_history = [{"role": "system", "content": system_prompt}]
|
| 36 |
return "", "" # Clear prompt and output, but not the API key
|
| 37 |
|
| 38 |
|
|
|
|
| 42 |
gr.Markdown("<center><h2>Arjun's Chatbot</h2></center>")
|
| 43 |
gr.Markdown("Hi there! I'm an AI assistant tasked with answering one question at a time. I'm instructed to provide complete and truthful responses, without any harmful or biased content. I have some memory issues which are work in progress, so I cannot remember previous conversations, yet!. Happy Chatting!")
|
| 44 |
prompt = gr.Textbox(label='Question', lines=2, max_lines=5, placeholder = 'Type your question here.')
|
| 45 |
+
conversation_display = gr.Textbox(label='Conversation History', lines=20, max_lines=30, placeholder='Conversation history will appear here...', interactive=False)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def update_conversation(prompt, history):
|
| 49 |
+
new_response = chat_with_llama(prompt)
|
| 50 |
+
updated_history = history + f"\nUser: {prompt}\n\nAssistant: "
|
| 51 |
+
for partial_response in new_response:
|
| 52 |
+
updated_history = history + f"\nUser: {prompt}\n\nAssistant: {partial_response}"
|
| 53 |
+
yield gr.update(value=updated_history)
|
| 54 |
|
| 55 |
with gr.Group():
|
| 56 |
with gr.Row():
|
| 57 |
submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
|
| 58 |
clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
submit_btn.click(fn=update_conversation, inputs = [prompt,conversation_display], outputs=[conversation_display])
|
| 61 |
+
clear_btn.click(fn=clear_fields,outputs=[prompt,conversation_display])
|
| 62 |
|
| 63 |
demo.launch()
|
| 64 |
#test comment
|