kshahnathwani commited on
Commit
7375397
·
verified ·
1 Parent(s): 9837755

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -46,7 +46,9 @@ Always respond with the chord name and a short explanation of the intervals.
46
 
47
  # Read token from Secrets (set in Space -> Settings -> Repository secrets)
48
  HF_TOKEN = os.environ.get("HF_TOKEN")
49
- client = InferenceClient(token=HF_TOKEN, model="google/flan-t5-small")
 
 
50
 
51
  def respond(message, history, system_message, max_tokens, temperature, top_p):
52
  if HF_TOKEN is None:
@@ -57,19 +59,17 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
57
  prompt = f"{system_message}\nUser: {message}\nAnswer:"
58
  response = ""
59
 
60
- for chunk in client.post(
61
- json={
62
- "inputs": prompt,
63
- "parameters": {
64
- "max_new_tokens": max_tokens,
65
- "temperature": temperature,
66
- "top_p": top_p,
67
- },
68
- "stream": True,
69
- }
70
  ):
71
- if "token" in chunk:
72
- token = chunk["token"]["text"]
 
73
  response += token
74
  yield response.strip()
75
 
@@ -81,7 +81,6 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
81
  yield f"⚠️ Error: {str(e)}\n\nTraceback:\n{tb}"
82
 
83
 
84
-
85
  chatbot = gr.ChatInterface(
86
  fn=respond,
87
  additional_inputs=[
 
46
 
47
  # Read token from Secrets (set in Space -> Settings -> Repository secrets)
48
  HF_TOKEN = os.environ.get("HF_TOKEN")
49
+
50
+ # Create inference client
51
+ client = InferenceClient(token=HF_TOKEN)
52
 
53
  def respond(message, history, system_message, max_tokens, temperature, top_p):
54
  if HF_TOKEN is None:
 
59
  prompt = f"{system_message}\nUser: {message}\nAnswer:"
60
  response = ""
61
 
62
+ for chunk in client.text_generation(
63
+ prompt,
64
+ model="google/flan-t5-small", # ✅ ensure correct model is used
65
+ max_new_tokens=max_tokens,
66
+ temperature=temperature,
67
+ top_p=top_p,
68
+ stream=True,
 
 
 
69
  ):
70
+ # chunks are StreamingResponse objects
71
+ if hasattr(chunk, "token") and chunk.token and chunk.token.text:
72
+ token = chunk.token.text
73
  response += token
74
  yield response.strip()
75
 
 
81
  yield f"⚠️ Error: {str(e)}\n\nTraceback:\n{tb}"
82
 
83
 
 
84
  chatbot = gr.ChatInterface(
85
  fn=respond,
86
  additional_inputs=[