implement model choose and more finished yipiieeee
This commit is contained in:
parent
da6c2faf59
commit
38d66af439
1 changed files with 11 additions and 4 deletions
15
app.py
15
app.py
|
@ -1,3 +1,4 @@
|
|||
import requests
|
||||
import gradio as gr
|
||||
from openai import OpenAI
|
||||
|
||||
|
@ -8,11 +9,16 @@ client = OpenAI(
|
|||
|
||||
chat_history = []
|
||||
|
||||
def ask(text):
|
||||
def get_models():
|
||||
response = requests.get("http://localhost:7869/api/tags")
|
||||
models = response.json()["models"]
|
||||
return [model["name"] for model in models]
|
||||
|
||||
def ask(text, model):
|
||||
global chat_history
|
||||
chat_history.append({"role": "user", "content": text})
|
||||
response = client.chat.completions.create(
|
||||
model="llama3.2",
|
||||
model=model,
|
||||
messages=chat_history
|
||||
)
|
||||
response_text = response.choices[0].message.content
|
||||
|
@ -34,13 +40,14 @@ def clear_history():
|
|||
|
||||
with gr.Blocks() as server:
|
||||
with gr.Tab("Chatbot LF6"):
|
||||
model_dropdown = gr.Dropdown(label="Select Model", choices=get_models(), value="llama3.2")
|
||||
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
|
||||
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=None, outputs=chatbox)
|
||||
|
||||
server.launch()
|
Loading…
Add table
Reference in a new issue