-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio_start.py
71 lines (53 loc) · 1.73 KB
/
gradio_start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import gradio as gr
import gradio_resources
from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1',
api_key='ollama', # Using your API key placeholder
)
models = ["llama3.1:70b", "llama3:70b", "gemma:2b", "bgGPT:Q4"]
def predict(message, history, system_prompt, model):
conversation = []
system_prompt = system_prompt.strip()
if len(system_prompt) > 0:
conversation.append({"role": "system", "content": system_prompt})
for human, assistant in history:
conversation.append({"role": "user", "content": human })
conversation.append({"role": "assistant", "content": assistant})
conversation.append({"role": "user", "content": message})
print(conversation)
stream = client.chat.completions.create(
model=model,
messages=conversation,
stream=True
)
partial_message = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
partial_message = partial_message + chunk.choices[0].delta.content
yield partial_message
demo = gr.ChatInterface(
fn=predict,
additional_inputs=[
gr.Textbox("", label="System Prompt"),
gr.Dropdown(
models,
value=models[0],
multiselect=False,
label="Model"
)
],
title="🦙 Llama Local",
head=gradio_resources.head(),
js='() => { document.title = "Llama Local"; }',
theme="monochrome",
submit_btn="Send",
retry_btn="Regenerate",
show_progress='full',
concurrency_limit=3,
fill_height=True,
autofocus=True
)
if __name__ == "__main__":
print("Starting Llama Local...")
demo.queue().launch(server_name="0.0.0.0", server_port=7860)