Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,6 @@ client = OpenAI(
|
|
| 11 |
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
| 12 |
)
|
| 13 |
|
| 14 |
-
FREE_MODELS = [
|
| 15 |
-
"meta-llama/llama-3.3-70b-instruct:free",
|
| 16 |
-
"google/gemma-3-27b-it:free",
|
| 17 |
-
"qwen/qwen3-32b:free",
|
| 18 |
-
"deepseek/deepseek-r1:free",
|
| 19 |
-
"mistralai/mistral-small-3.1-24b-instruct:free",
|
| 20 |
-
]
|
| 21 |
-
|
| 22 |
def clean_response(text: str) -> str:
|
| 23 |
lines = []
|
| 24 |
for line in text.splitlines():
|
|
@@ -31,50 +23,42 @@ def clean_response(text: str) -> str:
|
|
| 31 |
def chat(user_message: str, history: list):
|
| 32 |
history = history or []
|
| 33 |
|
| 34 |
-
# Build messages for the API
|
| 35 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 36 |
for msg in history:
|
| 37 |
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 38 |
messages.append({"role": "user", "content": user_message})
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
max_tokens=4096,
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
response_text = ""
|
| 53 |
-
for chunk in stream:
|
| 54 |
-
if chunk.choices[0].delta.content:
|
| 55 |
-
response_text += chunk.choices[0].delta.content
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
history.append({"role": "user", "content": user_message})
|
| 61 |
-
history.append({"role": "assistant", "content": response_text})
|
| 62 |
-
return history
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
with gr.Blocks(title="OpenRouter Free Chat") as demo:
|
| 77 |
-
gr.Markdown("## OpenRouter Free Chat (Auto
|
| 78 |
|
| 79 |
chatbot = gr.Chatbot(
|
| 80 |
label="Conversation",
|
|
|
|
| 11 |
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def clean_response(text: str) -> str:
|
| 15 |
lines = []
|
| 16 |
for line in text.splitlines():
|
|
|
|
| 23 |
def chat(user_message: str, history: list):
|
| 24 |
history = history or []
|
| 25 |
|
|
|
|
| 26 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 27 |
for msg in history:
|
| 28 |
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 29 |
messages.append({"role": "user", "content": user_message})
|
| 30 |
|
| 31 |
+
try:
|
| 32 |
+
# openrouter/free automatically picks a working free model
|
| 33 |
+
stream = client.chat.completions.create(
|
| 34 |
+
model="openrouter/free",
|
| 35 |
+
messages=messages,
|
| 36 |
+
stream=True,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
max_tokens=4096,
|
| 39 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
response_text = ""
|
| 42 |
+
for chunk in stream:
|
| 43 |
+
if chunk.choices[0].delta.content:
|
| 44 |
+
response_text += chunk.choices[0].delta.content
|
| 45 |
|
| 46 |
+
response_text = clean_response(response_text)
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
history.append({"role": "user", "content": user_message})
|
| 49 |
+
history.append({"role": "assistant", "content": response_text})
|
| 50 |
+
return history
|
| 51 |
|
| 52 |
+
except Exception as e:
|
| 53 |
+
history.append({"role": "user", "content": user_message})
|
| 54 |
+
history.append({
|
| 55 |
+
"role": "assistant",
|
| 56 |
+
"content": f"Error: {str(e)}"
|
| 57 |
+
})
|
| 58 |
+
return history
|
| 59 |
|
| 60 |
with gr.Blocks(title="OpenRouter Free Chat") as demo:
|
| 61 |
+
gr.Markdown("## OpenRouter Free Chat (Auto Free Model)")
|
| 62 |
|
| 63 |
chatbot = gr.Chatbot(
|
| 64 |
label="Conversation",
|