aimanathar commited on
Commit
c0e8472
·
verified ·
1 Parent(s): a4d7c0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -1,25 +1,40 @@
1
- import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
 
 
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def respond(message, history):
8
- client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
9
- messages = [{"role": "system", "content": "You are a friendly chatbot."}]
10
- for h in history:
11
- messages.append({"role": "user", "content": h[0]})
12
- messages.append({"role": "assistant", "content": h[1]})
13
- messages.append({"role": "user", "content": message})
14
 
15
- response = ""
16
- for msg in client.chat_completion(messages, max_tokens=200, stream=True):
17
- if msg.choices and msg.choices[0].delta.content:
18
- response += msg.choices[0].delta.content
19
- return response
20
 
21
- # 👇 is tarah karo taake API endpoint expose ho
22
- demo = gr.ChatInterface(respond)
23
 
24
  if __name__ == "__main__":
25
- demo.launch()
 
1
+ from flask import Flask, request, jsonify
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
+ app = Flask(__name__)
6
+
7
+ # Hugging Face Token (set in Space secrets as HF_TOKEN)
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
+ client = InferenceClient(model="openai/gpt-oss-20b", token=HF_TOKEN)
10
+
11
+ @app.route("/chat", methods=["POST"])
12
+ def chat():
13
+ try:
14
+ data = request.get_json()
15
+ user_message = data.get("message", "")
16
+
17
+ messages = [
18
+ {"role": "system", "content": "You are a friendly chatbot."},
19
+ {"role": "user", "content": user_message}
20
+ ]
21
+
22
+ response = ""
23
+ for msg in client.chat_completion(
24
+ messages,
25
+ max_tokens=256,
26
+ stream=True,
27
+ temperature=0.7,
28
+ top_p=0.95
29
+ ):
30
+ if msg.choices and msg.choices[0].delta.content:
31
+ response += msg.choices[0].delta.content
32
 
33
+ return jsonify({"reply": response})
 
 
 
 
 
 
34
 
35
+ except Exception as e:
36
+ return jsonify({"error": str(e)}), 500
 
 
 
37
 
 
 
38
 
39
  if __name__ == "__main__":
40
+ app.run(host="0.0.0.0", port=7860)