Spaces:
Running
Running
chat
#1
by harpertoken - opened
- README.md +8 -21
- app.py +37 -54
- requirements.txt +2 -1
README.md
CHANGED
|
@@ -1,25 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
emoji: 🤖
|
| 5 |
colorFrom: gray
|
| 6 |
-
colorTo:
|
| 7 |
sdk: gradio
|
| 8 |
-
sdk_version:
|
| 9 |
app_file: app.py
|
| 10 |
-
pinned:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
A Gradio interface over the [OpenRouter](https://openrouter.ai) chat API.
|
| 16 |
-
|
| 17 |
-
All generation is done by OpenRouter. This Space does not load a model from
|
| 18 |
-
`harpertoken`; it only forwards your message and renders the reply.
|
| 19 |
-
|
| 20 |
-
## Setup
|
| 21 |
-
|
| 22 |
-
1. Create an API key at [openrouter.ai](https://openrouter.ai)
|
| 23 |
-
2. Go to **Settings → Variables and secrets**
|
| 24 |
-
3. Add a secret named `OPENROUTER_API_KEY` with that key
|
| 25 |
-
4. Rebuild the Space
|
|
|
|
| 1 |
---
|
| 2 |
+
title: harpertoken gpt-oss-120b
|
| 3 |
+
emoji: 👋
|
|
|
|
| 4 |
colorFrom: gray
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.49.1
|
| 8 |
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: 120b gpt model with cerebras speeds
|
| 12 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -2,92 +2,75 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
-
from
|
| 6 |
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
client =
|
| 10 |
-
|
| 11 |
-
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
| 12 |
)
|
| 13 |
|
| 14 |
-
def clean_response(text
|
| 15 |
lines = []
|
| 16 |
for line in text.splitlines():
|
|
|
|
| 17 |
line = re.sub(r"^\s*[*#•\-]+\s*", "", line)
|
|
|
|
| 18 |
line = re.sub(r"\*\*(.*?)\*\*", r"\1", line)
|
| 19 |
-
|
| 20 |
-
# __init__ is a Python dunder, which shares the exact shape of
|
| 21 |
-
# __underline__ and cannot be told apart by position alone.
|
| 22 |
-
line = re.sub(r"(?<!\w)__([^_\n]*?\s[^_\n]*?)__(?!\w)", r"\1", line)
|
| 23 |
lines.append(line)
|
| 24 |
return "\n".join(lines)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def chat(user_message: str, history: list):
|
| 30 |
-
history = history or []
|
| 31 |
-
|
| 32 |
-
# Cap what is sent, not what is shown: unbounded history eventually exceeds
|
| 33 |
-
# the model's context window, and the failure then looks like a model error.
|
| 34 |
-
recent = history[-(MAX_SENT_TURNS * 2):]
|
| 35 |
-
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 36 |
-
for msg in recent:
|
| 37 |
-
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 38 |
-
messages.append({"role": "user", "content": user_message})
|
| 39 |
-
|
| 40 |
-
history = history + [{"role": "user", "content": user_message}]
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
-
# openrouter/free automatically picks a working free model
|
| 44 |
stream = client.chat.completions.create(
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
stream=True,
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
response_text = ""
|
| 53 |
-
history = history + [{"role": "assistant", "content": ""}]
|
| 54 |
for chunk in stream:
|
| 55 |
-
if chunk.choices
|
| 56 |
response_text += chunk.choices[0].delta.content
|
| 57 |
-
history[-1]["content"] = response_text
|
| 58 |
-
yield history
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
-
history
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
with gr.Blocks(title="OpenRouter Chat") as demo:
|
| 71 |
-
gr.Markdown("## OpenRouter Chat")
|
| 72 |
-
|
| 73 |
chatbot = gr.Chatbot(
|
| 74 |
label="Conversation",
|
| 75 |
-
height=600
|
|
|
|
| 76 |
)
|
| 77 |
-
|
| 78 |
with gr.Row():
|
| 79 |
msg = gr.Textbox(
|
| 80 |
label="Ask something",
|
| 81 |
placeholder="Type your prompt and press Enter",
|
| 82 |
-
scale=4
|
| 83 |
-
container=False
|
| 84 |
)
|
| 85 |
-
clear = gr.Button("Clear"
|
| 86 |
|
| 87 |
-
msg.submit(chat, [msg, chatbot], chatbot)
|
| 88 |
-
lambda: "", None, msg
|
| 89 |
-
)
|
| 90 |
clear.click(lambda: [], None, chatbot)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
demo.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from cerebras.cloud.sdk import Cerebras
|
| 6 |
|
| 7 |
+
# Load environment variables
|
| 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 (*, #, •, -) at line start
|
| 18 |
line = re.sub(r"^\s*[*#•\-]+\s*", "", line)
|
| 19 |
+
# Remove markdown bold
|
| 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(user_message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
|
|
|
| 27 |
stream = client.chat.completions.create(
|
| 28 |
+
messages=[
|
| 29 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 30 |
+
{"role": "user", "content": user_message}
|
| 31 |
+
],
|
| 32 |
+
model="gpt-oss-120b",
|
| 33 |
stream=True,
|
| 34 |
+
max_completion_tokens=65536,
|
| 35 |
+
temperature=1,
|
| 36 |
+
top_p=1,
|
| 37 |
+
reasoning_effort="medium"
|
| 38 |
)
|
| 39 |
|
| 40 |
response_text = ""
|
|
|
|
| 41 |
for chunk in stream:
|
| 42 |
+
if chunk.choices[0].delta.content:
|
| 43 |
response_text += chunk.choices[0].delta.content
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
response_text = clean_response(response_text)
|
| 46 |
+
|
| 47 |
+
history = history or []
|
| 48 |
+
history.append((user_message, response_text))
|
| 49 |
+
return history
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
history = history or []
|
| 53 |
+
history.append((user_message, f"Error: {str(e)}"))
|
| 54 |
+
return history
|
| 55 |
+
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("## Chat UI (Long responses stay open)")
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
chatbot = gr.Chatbot(
|
| 60 |
label="Conversation",
|
| 61 |
+
height=600,
|
| 62 |
+
show_copy_button=True
|
| 63 |
)
|
| 64 |
+
|
| 65 |
with gr.Row():
|
| 66 |
msg = gr.Textbox(
|
| 67 |
label="Ask something",
|
| 68 |
placeholder="Type your prompt and press Enter",
|
| 69 |
+
scale=4
|
|
|
|
| 70 |
)
|
| 71 |
+
clear = gr.Button("Clear")
|
| 72 |
|
| 73 |
+
msg.submit(chat, [msg, chatbot], chatbot)
|
|
|
|
|
|
|
| 74 |
clear.click(lambda: [], None, chatbot)
|
| 75 |
|
| 76 |
+
demo.launch()
|
|
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
python-dotenv
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
cerebras-cloud-sdk
|
| 3 |
python-dotenv
|