sherazanjum1976 commited on
Commit
1f21130
·
verified ·
1 Parent(s): 9b4315c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -1,17 +1,28 @@
1
  import os
2
  import gradio as gr
3
- from openai import OpenAI
4
 
5
- # Initialize client with Hugging Face OpenAI-compatible endpoint
6
- client = OpenAI(
7
- base_url="https://router.huggingface.co/v1",
8
- api_key=os.environ["HF_TOKEN"], # Hugging Face token stored in Secrets
9
- )
10
 
11
- # Function to handle chat conversation
 
 
 
 
 
 
 
 
 
 
12
  def chat_with_deepseek(message, history):
 
 
 
13
  try:
14
- # Convert Gradio history into OpenAI format
15
  messages = []
16
  for user_msg, bot_msg in history:
17
  messages.append({"role": "user", "content": user_msg})
@@ -75,7 +86,7 @@ with gr.Blocks(theme="soft", css="""
75
  )
76
  submit_btn = gr.Button("Send", elem_id="submitbtn")
77
 
78
- # Event: when user submits input
79
  submit_btn.click(
80
  fn=chat_with_deepseek,
81
  inputs=[user_input, chatbot],
 
1
  import os
2
  import gradio as gr
 
3
 
4
+ try:
5
+ from openai import OpenAI
6
+ except ImportError:
7
+ raise RuntimeError("⚠️ Missing dependency. Make sure 'openai' is listed in requirements.txt")
 
8
 
9
+ # Check if HF_TOKEN exists
10
+ api_key = os.getenv("HF_TOKEN")
11
+
12
+ client = None
13
+ if api_key:
14
+ client = OpenAI(
15
+ base_url="https://router.huggingface.co/v1",
16
+ api_key=api_key,
17
+ )
18
+
19
+ # Function to handle chat
20
  def chat_with_deepseek(message, history):
21
+ if not client:
22
+ return "❌ HF_TOKEN not found. Please set it in 'Settings → Repository secrets' of your Space."
23
+
24
  try:
25
+ # Convert Gradio history to OpenAI messages format
26
  messages = []
27
  for user_msg, bot_msg in history:
28
  messages.append({"role": "user", "content": user_msg})
 
86
  )
87
  submit_btn = gr.Button("Send", elem_id="submitbtn")
88
 
89
+ # Bind events
90
  submit_btn.click(
91
  fn=chat_with_deepseek,
92
  inputs=[user_input, chatbot],