ai-chatbot/app.py

46 lines
No EOL
1.6 KiB
Python

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):
global chat_history
chat_history.append({"role": "user", "content": text})
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 ""
with gr.Blocks() as server:
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...")
submit_button = gr.Button("Submit")
clear_button = gr.Button("Clear")
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()