Introduce OOP!
This commit is contained in:
parent
f2aa98b712
commit
67d2257693
1 changed files with 46 additions and 43 deletions
89
app.py
89
app.py
|
@ -2,52 +2,55 @@ import requests
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
client = OpenAI(
|
class ChatbotApp:
|
||||||
base_url = 'http://localhost:7869/v1',
|
def __init__(self):
|
||||||
api_key='ollama', # required, but unused
|
self.client = OpenAI(
|
||||||
)
|
base_url='http://localhost:7869/v1',
|
||||||
|
api_key='ollama', # required, but unused
|
||||||
|
)
|
||||||
|
self.chat_history = []
|
||||||
|
|
||||||
chat_history = []
|
def get_models(self):
|
||||||
|
response = requests.get("http://localhost:7869/api/tags")
|
||||||
|
models = response.json()["models"]
|
||||||
|
return [model["name"] for model in models]
|
||||||
|
|
||||||
def get_models():
|
def ask(self, text, model):
|
||||||
response = requests.get("http://localhost:7869/api/tags")
|
self.chat_history.append({"role": "user", "content": text})
|
||||||
models = response.json()["models"]
|
response = self.client.chat.completions.create(
|
||||||
return [model["name"] for model in models]
|
model=model,
|
||||||
|
messages=self.chat_history
|
||||||
|
)
|
||||||
|
response_text = response.choices[0].message.content
|
||||||
|
self.chat_history.append({"role": "assistant", "content": response_text})
|
||||||
|
formatted_history = "\n".join([f"**{msg['role'].capitalize()}**: {msg['content']}" for msg in self.chat_history])
|
||||||
|
print("Formatted History: ", formatted_history)
|
||||||
|
return formatted_history, ""
|
||||||
|
|
||||||
def ask(text, model):
|
def clear_history(self, model):
|
||||||
global chat_history
|
self.chat_history = []
|
||||||
chat_history.append({"role": "user", "content": text})
|
# Reset the conversation on the server side
|
||||||
response = client.chat.completions.create(
|
self.client.chat.completions.create(
|
||||||
model=model,
|
model=model,
|
||||||
messages=chat_history
|
messages=[{"role": "system", "content": "Reset conversation"}]
|
||||||
)
|
)
|
||||||
response_text = response.choices[0].message.content
|
return ""
|
||||||
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(model):
|
def launch(self):
|
||||||
global chat_history
|
with gr.Blocks() as server:
|
||||||
chat_history = []
|
with gr.Tab("Chatbot LF6"):
|
||||||
# Reset the conversation on the server side
|
model_dropdown = gr.Dropdown(label="Select Model", choices=self.get_models())
|
||||||
client.chat.completions.create(
|
chatbox = gr.Textbox(label="Chat History", lines=10, interactive=False)
|
||||||
model=model,
|
input_text = gr.Textbox(label="Input Text", placeholder="Enter your text here...")
|
||||||
messages=[{"role": "system", "content": "Reset conversation"}]
|
submit_button = gr.Button("Submit")
|
||||||
)
|
clear_button = gr.Button("Clear")
|
||||||
return ""
|
|
||||||
|
submit_button.click(fn=lambda text, model: self.ask(text, model), inputs=[input_text, model_dropdown], outputs=[chatbox, input_text])
|
||||||
|
input_text.submit(fn=lambda text, model: self.ask(text, model), inputs=[input_text, model_dropdown], outputs=[chatbox, input_text]) # Submit on Enter key press
|
||||||
|
clear_button.click(fn=self.clear_history, inputs=model_dropdown, outputs=chatbox)
|
||||||
|
|
||||||
with gr.Blocks() as server:
|
server.launch()
|
||||||
with gr.Tab("Chatbot LF6"):
|
|
||||||
model_dropdown = gr.Dropdown(label="Select Model", choices=get_models())
|
|
||||||
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=lambda text, model: ask(text, model), inputs=[input_text, model_dropdown], outputs=[chatbox, input_text])
|
|
||||||
input_text.submit(fn=lambda text, model: ask(text, model), inputs=[input_text, model_dropdown], outputs=[chatbox, input_text]) # Submit on Enter key press
|
|
||||||
clear_button.click(fn=clear_history, inputs=model_dropdown, outputs=chatbox)
|
|
||||||
|
|
||||||
server.launch()
|
if __name__ == "__main__":
|
||||||
|
app = ChatbotApp()
|
||||||
|
app.launch()
|
Loading…
Add table
Reference in a new issue