Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +83 -0
- requirements (1).txt +2 -0
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Tuple, Dict, Generator
|
2 |
+
from langchain.llms import OpenAI
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
model_name = "gpt-3.5-turbo"
|
6 |
+
LLM = OpenAI(model_name=model_name, temperature=0.1)
|
7 |
+
|
8 |
+
def create_history_messages(history: List[Tuple[str, str]]) -> List[dict]:
|
9 |
+
history_messages = [{"role": "user", "content": m[0]} for m in history]
|
10 |
+
history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
|
11 |
+
return history_messages
|
12 |
+
|
13 |
+
def create_formatted_history(history_messages: List[dict]) -> List[Tuple[str, str]]:
|
14 |
+
formatted_history = []
|
15 |
+
user_messages = []
|
16 |
+
assistant_messages = []
|
17 |
+
|
18 |
+
for message in history_messages:
|
19 |
+
if message["role"] == "user":
|
20 |
+
user_messages.append(message["content"])
|
21 |
+
elif message["role"] == "assistant":
|
22 |
+
assistant_messages.append(message["content"])
|
23 |
+
|
24 |
+
if user_messages and assistant_messages:
|
25 |
+
formatted_history.append(
|
26 |
+
("".join(user_messages), "".join(assistant_messages))
|
27 |
+
)
|
28 |
+
user_messages = []
|
29 |
+
assistant_messages = []
|
30 |
+
|
31 |
+
# append any remaining messages
|
32 |
+
if user_messages:
|
33 |
+
formatted_history.append(("".join(user_messages), None))
|
34 |
+
elif assistant_messages:
|
35 |
+
formatted_history.append((None, "".join(assistant_messages)))
|
36 |
+
|
37 |
+
return formatted_history
|
38 |
+
|
39 |
+
def chat(
|
40 |
+
message: str, state: List[Dict[str, str]], client = LLM.client
|
41 |
+
) -> Generator[Tuple[List[Tuple[str, str]], List[Dict[str, str]]], None, None]:
|
42 |
+
history_messages = state
|
43 |
+
if history_messages == None:
|
44 |
+
history_messages = []
|
45 |
+
history_messages.append({"role": "system", "content": "A helpful assistant."})
|
46 |
+
|
47 |
+
history_messages.append({"role": "user", "content": message})
|
48 |
+
# We have no content for the assistant's response yet but we will update this:
|
49 |
+
history_messages.append({"role": "assistant", "content": ""})
|
50 |
+
|
51 |
+
response_message = ""
|
52 |
+
|
53 |
+
chat_generator = client.create(
|
54 |
+
messages=history_messages, stream=True, model=model_name
|
55 |
+
)
|
56 |
+
|
57 |
+
for chunk in chat_generator:
|
58 |
+
if "choices" in chunk:
|
59 |
+
for choice in chunk["choices"]:
|
60 |
+
if "delta" in choice and "content" in choice["delta"]:
|
61 |
+
new_token = choice["delta"]["content"]
|
62 |
+
# Add the latest token:
|
63 |
+
response_message += new_token
|
64 |
+
# Update the assistant's response in our model:
|
65 |
+
history_messages[-1]["content"] = response_message
|
66 |
+
|
67 |
+
if "finish_reason" in choice and choice["finish_reason"] == "stop":
|
68 |
+
break
|
69 |
+
formatted_history = create_formatted_history(history_messages)
|
70 |
+
yield formatted_history, history_messages
|
71 |
+
|
72 |
+
chatbot = gr.Chatbot(label="Chat").style(color_map=("yellow", "purple"))
|
73 |
+
iface = gr.Interface(
|
74 |
+
fn=chat,
|
75 |
+
inputs=[
|
76 |
+
gr.Textbox(placeholder="Hello! How are you? etc.", label="Message"),
|
77 |
+
"state",
|
78 |
+
],
|
79 |
+
outputs=[chatbot, "state"],
|
80 |
+
allow_flagging="never",
|
81 |
+
)
|
82 |
+
|
83 |
+
iface.queue().launch()
|
requirements (1).txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|