Muhammadidrees commited on
Commit
27ee687
·
verified ·
1 Parent(s): 01d4e47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -15,20 +15,25 @@ client = OpenAI(
15
 
16
  # --- Chat handler ---
17
  def chat_with_model(message, history):
18
- # Convert message history into OpenAI-style messages
19
  messages = []
20
 
21
  if history:
22
  for msg in history:
23
- if isinstance(msg, dict): # New Gradio message format
24
- messages.append(msg)
25
- elif isinstance(msg, (list, tuple)) and len(msg) == 2: # Old format fallback
 
 
 
 
 
26
  messages.append({"role": "user", "content": msg[0]})
27
  messages.append({"role": "assistant", "content": msg[1]})
28
 
 
29
  messages.append({"role": "user", "content": message})
30
 
31
- # Query model
32
  try:
33
  response = client.chat.completions.create(
34
  model="openai/gpt-oss-120b:fireworks-ai",
@@ -46,8 +51,8 @@ chatbot_ui = gr.ChatInterface(
46
  fn=chat_with_model,
47
  title="🧠 GPT-OSS 120B (Fireworks)",
48
  description="Chat with the OSS 120B model hosted via Hugging Face router.",
49
- examples=["Hello!", "Tell me a joke.", "Explain quantum computing in simple terms."],
50
- type="messages", # required to use new format
51
  )
52
 
53
  if __name__ == "__main__":
 
15
 
16
  # --- Chat handler ---
17
  def chat_with_model(message, history):
18
+ # Build messages list safely
19
  messages = []
20
 
21
  if history:
22
  for msg in history:
23
+ # Handle both dict and tuple formats
24
+ if isinstance(msg, dict):
25
+ # Keep only allowed keys
26
+ messages.append({
27
+ "role": msg.get("role", "user"),
28
+ "content": msg.get("content", "")
29
+ })
30
+ elif isinstance(msg, (list, tuple)) and len(msg) == 2:
31
  messages.append({"role": "user", "content": msg[0]})
32
  messages.append({"role": "assistant", "content": msg[1]})
33
 
34
+ # Add latest user message
35
  messages.append({"role": "user", "content": message})
36
 
 
37
  try:
38
  response = client.chat.completions.create(
39
  model="openai/gpt-oss-120b:fireworks-ai",
 
51
  fn=chat_with_model,
52
  title="🧠 GPT-OSS 120B (Fireworks)",
53
  description="Chat with the OSS 120B model hosted via Hugging Face router.",
54
+ examples=["Hello!", "Tell me a joke.", "Explain AI in simple terms."],
55
+ type="messages", # required
56
  )
57
 
58
  if __name__ == "__main__":