abdullahalioo commited on
Commit
cc3751a
·
verified ·
1 Parent(s): da0c5d9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -31
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
- # FastAPI setup
33
- app = FastAPI()
34
-
35
- # Enable CORS
36
- app.add_middleware(
37
- CORSMiddleware,
38
- allow_origins=["*"],
39
- allow_credentials=True,
40
- allow_methods=["*"],
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
+