Little happy chatbot is finished yipiieee
This commit is contained in:
parent
39c46bdb7b
commit
da6c2faf59
1 changed files with 35 additions and 11 deletions
46
app.py
46
app.py
|
@ -1,22 +1,46 @@
|
||||||
import requests
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
|
from openai import OpenAI
|
||||||
|
|
||||||
|
client = OpenAI(
|
||||||
|
base_url = 'http://localhost:7869/v1',
|
||||||
|
api_key='ollama', # required, but unused
|
||||||
|
)
|
||||||
|
|
||||||
|
chat_history = []
|
||||||
|
|
||||||
def ask(text):
|
def ask(text):
|
||||||
url = "http://localhost:7869/ "
|
global chat_history
|
||||||
payload = {"text": text}
|
chat_history.append({"role": "user", "content": text})
|
||||||
response = requests.post(url, json=payload)
|
response = client.chat.completions.create(
|
||||||
|
model="llama3.2",
|
||||||
|
messages=chat_history
|
||||||
|
)
|
||||||
|
response_text = response.choices[0].message.content
|
||||||
|
chat_history.append({"role": "assistant", "content": response_text})
|
||||||
|
formatted_history = "\n".join([f"**{msg['role'].capitalize()}**: {msg['content']}" for msg in chat_history])
|
||||||
|
print("Formatted History: ", formatted_history)
|
||||||
|
return formatted_history, ""
|
||||||
|
|
||||||
|
def clear_history():
|
||||||
|
global chat_history
|
||||||
|
chat_history = []
|
||||||
|
# Reset the conversation on the server side
|
||||||
|
client.chat.completions.create(
|
||||||
|
model="llama3.2",
|
||||||
|
messages=[{"role": "system", "content": "Reset conversation"}]
|
||||||
|
)
|
||||||
|
return ""
|
||||||
|
|
||||||
if response.status_code == 200:
|
|
||||||
return response.json().get("response", "No response from API")
|
|
||||||
else:
|
|
||||||
return f"Error: {response.status_code}"
|
|
||||||
|
|
||||||
with gr.Blocks() as server:
|
with gr.Blocks() as server:
|
||||||
with gr.Tab("LLM Inferencing"):
|
with gr.Tab("Chatbot LF6"):
|
||||||
|
chatbox = gr.Textbox(label="Chat History", lines=10, interactive=False)
|
||||||
input_text = gr.Textbox(label="Input Text", placeholder="Enter your text here...")
|
input_text = gr.Textbox(label="Input Text", placeholder="Enter your text here...")
|
||||||
output_text = gr.Textbox(label="Output Text")
|
|
||||||
submit_button = gr.Button("Submit")
|
submit_button = gr.Button("Submit")
|
||||||
|
clear_button = gr.Button("Clear")
|
||||||
|
|
||||||
submit_button.click(fn=ask, inputs=input_text, outputs=output_text)
|
submit_button.click(fn=ask, inputs=input_text, outputs=[chatbox, input_text])
|
||||||
|
input_text.submit(fn=ask, inputs=input_text, outputs=[chatbox, input_text]) # Submit on Enter key press
|
||||||
|
clear_button.click(fn=clear_history, inputs=None, outputs=chatbox)
|
||||||
|
|
||||||
server.launch()
|
server.launch()
|
Loading…
Add table
Reference in a new issue