2024-02-25 21:09:13 -05:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
|
from openai import OpenAI
|
|
|
|
|
|
2024-07-09 12:20:17 -04:00
|
|
|
client = OpenAI(base_url="http://localhost:8000/v1", api_key="llama.cpp")
|
2024-02-25 21:09:13 -05:00
|
|
|
|
|
|
|
|
model = "gpt-3.5-turbo"
|
|
|
|
|
|
2024-07-09 12:20:17 -04:00
|
|
|
|
2024-02-25 21:09:13 -05:00
|
|
|
def predict(message, history):
|
|
|
|
|
messages = []
|
|
|
|
|
|
|
|
|
|
for user_message, assistant_message in history:
|
|
|
|
|
messages.append({"role": "user", "content": user_message})
|
|
|
|
|
messages.append({"role": "assistant", "content": assistant_message})
|
2024-07-09 12:20:17 -04:00
|
|
|
|
2024-02-25 21:09:13 -05:00
|
|
|
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
response = client.chat.completions.create(
|
2024-07-09 12:20:17 -04:00
|
|
|
model=model, messages=messages, stream=True
|
2024-02-25 21:09:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
text = ""
|
|
|
|
|
for chunk in response:
|
|
|
|
|
content = chunk.choices[0].delta.content
|
|
|
|
|
if content:
|
|
|
|
|
text += content
|
|
|
|
|
yield text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
js = """function () {
|
|
|
|
|
gradioURL = window.location.href
|
|
|
|
|
if (!gradioURL.endsWith('?__theme=dark')) {
|
|
|
|
|
window.location.replace(gradioURL + '?__theme=dark');
|
|
|
|
|
}
|
|
|
|
|
}"""
|
|
|
|
|
|
|
|
|
|
css = """
|
|
|
|
|
footer {
|
|
|
|
|
visibility: hidden;
|
|
|
|
|
}
|
|
|
|
|
full-height {
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft(), js=js, css=css, fill_height=True) as demo:
|
2024-07-09 12:20:17 -04:00
|
|
|
gr.ChatInterface(
|
|
|
|
|
predict,
|
|
|
|
|
fill_height=True,
|
|
|
|
|
examples=[
|
|
|
|
|
"What is the capital of France?",
|
|
|
|
|
"Who was the first person on the moon?",
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-02-25 21:09:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
demo.launch()
|