Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -29,34 +29,12 @@ chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
|
29 |
ASSISTANT_ID = "66017fca58d60bd7d5c5c26c" # Replace if needed
|
30 |
chatbot.new_conversation(assistant=ASSISTANT_ID, switch_to=True)
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
allow_headers=["*"],
|
42 |
-
)
|
43 |
-
|
44 |
-
# Request model
|
45 |
-
class Question(BaseModel):
|
46 |
-
question: str
|
47 |
-
|
48 |
-
# Token stream function
|
49 |
-
async def generate_response_stream(prompt: str):
|
50 |
-
for chunk in chatbot.chat(prompt, stream=True):
|
51 |
-
token = chunk.get("token", "")
|
52 |
-
if token:
|
53 |
-
yield token
|
54 |
-
await asyncio.sleep(0.02)
|
55 |
-
|
56 |
-
# Endpoint
|
57 |
-
@app.post("/ask")
|
58 |
-
async def ask(question: Question):
|
59 |
-
return StreamingResponse(
|
60 |
-
generate_response_stream(question.question),
|
61 |
-
media_type="text/plain"
|
62 |
-
)
|
|
|
29 |
ASSISTANT_ID = "66017fca58d60bd7d5c5c26c" # Replace if needed
|
30 |
chatbot.new_conversation(assistant=ASSISTANT_ID, switch_to=True)
|
31 |
|
32 |
+
# 🔁 Stream response character-by-character as it generates
|
33 |
+
print("Assistant:", end=" ", flush=True)
|
34 |
+
for token in chatbot.chat("Hello, how can I help you today?", stream=True):
|
35 |
+
print(token, end="", flush=True)
|
36 |
+
|
37 |
+
# Optionally: web search example
|
38 |
+
# response = chatbot.chat("How many models stored in huggingface?", web_search=True)
|
39 |
+
# print("\n\nWeb Search Result:", response.wait_until_done())
|
40 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|