Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,32 +52,34 @@ client = InferenceClient(token=HF_TOKEN)
|
|
| 52 |
|
| 53 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 54 |
if HF_TOKEN is None:
|
| 55 |
-
|
| 56 |
-
return
|
| 57 |
|
| 58 |
try:
|
| 59 |
prompt = f"{system_message}\nUser: {message}\nAnswer:"
|
| 60 |
-
response = ""
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
prompt,
|
| 64 |
-
model="gpt2",
|
| 65 |
max_new_tokens=max_tokens,
|
| 66 |
temperature=temperature,
|
| 67 |
top_p=top_p,
|
| 68 |
-
stream=
|
| 69 |
-
)
|
| 70 |
-
if hasattr(chunk, "token") and chunk.token and chunk.token.text:
|
| 71 |
-
token = chunk.token.text
|
| 72 |
-
response += token
|
| 73 |
-
yield response.strip()
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
tb = traceback.format_exc()
|
| 80 |
-
|
| 81 |
|
| 82 |
|
| 83 |
chatbot = gr.ChatInterface(
|
|
@@ -92,7 +94,7 @@ chatbot = gr.ChatInterface(
|
|
| 92 |
)
|
| 93 |
|
| 94 |
with gr.Blocks(css=fancy_css) as demo:
|
| 95 |
-
gr.Markdown("<h1 id='title'>🎶 Chord Bot (API-based
|
| 96 |
chatbot.render()
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
|
|
|
| 52 |
|
| 53 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 54 |
if HF_TOKEN is None:
|
| 55 |
+
return "⚠️ No HF_TOKEN found. Please add it in your Space secrets."
|
|
|
|
| 56 |
|
| 57 |
try:
|
| 58 |
prompt = f"{system_message}\nUser: {message}\nAnswer:"
|
|
|
|
| 59 |
|
| 60 |
+
# Non-streaming inference call
|
| 61 |
+
output = client.text_generation(
|
| 62 |
prompt,
|
| 63 |
+
model="gpt2", # ✅ small model, works with Inference API
|
| 64 |
max_new_tokens=max_tokens,
|
| 65 |
temperature=temperature,
|
| 66 |
top_p=top_p,
|
| 67 |
+
stream=False, # 🔑 disable streaming
|
| 68 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# Extract text
|
| 71 |
+
if hasattr(output, "generated_text"):
|
| 72 |
+
response = output.generated_text
|
| 73 |
+
elif isinstance(output, dict) and "generated_text" in output:
|
| 74 |
+
response = output["generated_text"]
|
| 75 |
+
else:
|
| 76 |
+
response = str(output)
|
| 77 |
+
|
| 78 |
+
return response.strip()
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
tb = traceback.format_exc()
|
| 82 |
+
return f"⚠️ Error: {str(e)}\n\nTraceback:\n{tb}"
|
| 83 |
|
| 84 |
|
| 85 |
chatbot = gr.ChatInterface(
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
with gr.Blocks(css=fancy_css) as demo:
|
| 97 |
+
gr.Markdown("<h1 id='title'>🎶 Chord Bot (API-based, GPT-2) 🎶</h1>")
|
| 98 |
chatbot.render()
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|