Spaces:
Sleeping
Sleeping
FunPawsie
commited on
Commit
·
4aae1ac
1
Parent(s):
9fdc708
first commit
Browse files- app.py +37 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
YOUR_API_KEY = "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"
|
5 |
+
|
6 |
+
def predict(message, history):
|
7 |
+
messages = [
|
8 |
+
{
|
9 |
+
"role": "system",
|
10 |
+
"content": (
|
11 |
+
"You are an artificial intelligence assistant and you need to "
|
12 |
+
"engage in a helpful, detailed, polite conversation with a user."
|
13 |
+
),
|
14 |
+
},
|
15 |
+
]
|
16 |
+
|
17 |
+
for human, assistant in history:
|
18 |
+
messages.append({"role": "user", "content": human})
|
19 |
+
messages.append({"role": "assistant", "content": assistant})
|
20 |
+
|
21 |
+
messages.append({"role": "user", "content": message})
|
22 |
+
|
23 |
+
client = OpenAI(api_key=YOUR_API_KEY, base_url="https://api.perplexity.ai")
|
24 |
+
|
25 |
+
response_stream = client.chat.completions.create(
|
26 |
+
model="sonar-small-online",
|
27 |
+
messages=messages,
|
28 |
+
stream=True,
|
29 |
+
)
|
30 |
+
|
31 |
+
partial_message = ""
|
32 |
+
for response in response_stream:
|
33 |
+
if response.choices[0].finish_reason == "stop":
|
34 |
+
partial_message += response.choices[0].text.strip()
|
35 |
+
yield partial_message
|
36 |
+
|
37 |
+
gr.Interface(fn=predict, inputs="text", outputs="text").launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
openai
|