Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from cerebras.cloud.sdk import Cerebras
|
6 |
+
|
7 |
+
# Load environment variables from .env file
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
client = Cerebras(
|
11 |
+
api_key=os.environ.get("CEREBRAS_API_KEY")
|
12 |
+
)
|
13 |
+
|
14 |
+
def clean_response(text):
|
15 |
+
lines = []
|
16 |
+
for line in text.splitlines():
|
17 |
+
# Remove bullets like *, #, •, - from the beginning (not numbered lists)
|
18 |
+
line = re.sub(r"^\s*[*#•\-]+\s*", "", line)
|
19 |
+
# Remove markdown bold formatting
|
20 |
+
line = re.sub(r"\*\*(.*?)\*\*", r"\1", line)
|
21 |
+
line = re.sub(r"__(.*?)__", r"\1", line)
|
22 |
+
lines.append(line)
|
23 |
+
return "\n".join(lines)
|
24 |
+
|
25 |
+
def chat(prompt, _):
|
26 |
+
try:
|
27 |
+
stream = client.chat.completions.create(
|
28 |
+
messages=[
|
29 |
+
{
|
30 |
+
"role": "system",
|
31 |
+
"content": "You are a helpful assistant."
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"role": "user",
|
35 |
+
"content": prompt
|
36 |
+
}
|
37 |
+
],
|
38 |
+
model="gpt-oss-120b",
|
39 |
+
stream=True,
|
40 |
+
max_completion_tokens=65536,
|
41 |
+
temperature=1,
|
42 |
+
top_p=1,
|
43 |
+
reasoning_effort="medium"
|
44 |
+
)
|
45 |
+
|
46 |
+
response_text = ""
|
47 |
+
for chunk in stream:
|
48 |
+
if chunk.choices[0].delta.content:
|
49 |
+
response_text += chunk.choices[0].delta.content
|
50 |
+
|
51 |
+
return clean_response(response_text)
|
52 |
+
except Exception as e:
|
53 |
+
return f"Error: {str(e)}"
|
54 |
+
|
55 |
+
gr.Interface(
|
56 |
+
fn=chat,
|
57 |
+
inputs=[gr.Textbox(label="Ask something"), gr.Radio(["English"], visible=False)],
|
58 |
+
outputs=gr.Textbox(label="Response")
|
59 |
+
).launch()
|