Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
client = InferenceClient("
|
| 7 |
|
| 8 |
-
# Your
|
| 9 |
FAQ_CONTEXT = """
|
| 10 |
An AI FAQ Agent can be adapted for almost any website where users have questions, need guidance, or require support. If people visit your site and need to find information, an FAQ agent can help. Here is a breakdown of the most common types of websites that benefit from an FAQ Agent, categorized by industry:
|
| 11 |
|
|
@@ -74,35 +74,32 @@ def respond(
|
|
| 74 |
|
| 75 |
messages = [{"role": "system", "content": full_system_prompt}]
|
| 76 |
|
| 77 |
-
#
|
| 78 |
if history:
|
| 79 |
for val in history:
|
| 80 |
-
# Safe parsing for Gradio 5/6 ChatMessage objects
|
| 81 |
if hasattr(val, "role") and hasattr(val, "content"):
|
| 82 |
role = val.role
|
| 83 |
content_str = extract_text(val.content)
|
| 84 |
if role and content_str:
|
| 85 |
messages.append({"role": role, "content": content_str})
|
| 86 |
-
# Safe parsing for native dictionaries
|
| 87 |
elif isinstance(val, dict) and "role" in val:
|
| 88 |
role = val.get("role")
|
| 89 |
content_str = extract_text(val.get("content") or val.get("text"))
|
| 90 |
if role and content_str:
|
| 91 |
messages.append({"role": role, "content": content_str})
|
| 92 |
-
# Safe parsing for older Gradio tuple logs (user, assistant)
|
| 93 |
elif isinstance(val, (list, tuple)) and len(val) >= 2:
|
| 94 |
if val[0]:
|
| 95 |
messages.append({"role": "user", "content": extract_text(val[0])})
|
| 96 |
if val[1]:
|
| 97 |
messages.append({"role": "assistant", "content": extract_text(val[1])})
|
| 98 |
|
| 99 |
-
#
|
| 100 |
user_content_str = extract_text(message)
|
| 101 |
messages.append({"role": "user", "content": user_content_str})
|
| 102 |
|
| 103 |
response = ""
|
| 104 |
|
| 105 |
-
#
|
| 106 |
try:
|
| 107 |
for message_chunk in client.chat_completion(
|
| 108 |
messages,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# CHANGED: Replaced 'zephyr-7b-beta' with 'Qwen2.5-7B-Instruct' which is fully supported
|
| 5 |
+
# on Hugging Face's serverless Inference infrastructure out-of-the-box.
|
| 6 |
+
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 7 |
|
| 8 |
+
# Your AI FAQ Agent Knowledge Base context
|
| 9 |
FAQ_CONTEXT = """
|
| 10 |
An AI FAQ Agent can be adapted for almost any website where users have questions, need guidance, or require support. If people visit your site and need to find information, an FAQ agent can help. Here is a breakdown of the most common types of websites that benefit from an FAQ Agent, categorized by industry:
|
| 11 |
|
|
|
|
| 74 |
|
| 75 |
messages = [{"role": "system", "content": full_system_prompt}]
|
| 76 |
|
| 77 |
+
# Cleanse and parse conversation history safely (Fixes the ChatMessage .get() error)
|
| 78 |
if history:
|
| 79 |
for val in history:
|
|
|
|
| 80 |
if hasattr(val, "role") and hasattr(val, "content"):
|
| 81 |
role = val.role
|
| 82 |
content_str = extract_text(val.content)
|
| 83 |
if role and content_str:
|
| 84 |
messages.append({"role": role, "content": content_str})
|
|
|
|
| 85 |
elif isinstance(val, dict) and "role" in val:
|
| 86 |
role = val.get("role")
|
| 87 |
content_str = extract_text(val.get("content") or val.get("text"))
|
| 88 |
if role and content_str:
|
| 89 |
messages.append({"role": role, "content": content_str})
|
|
|
|
| 90 |
elif isinstance(val, (list, tuple)) and len(val) >= 2:
|
| 91 |
if val[0]:
|
| 92 |
messages.append({"role": "user", "content": extract_text(val[0])})
|
| 93 |
if val[1]:
|
| 94 |
messages.append({"role": "assistant", "content": extract_text(val[1])})
|
| 95 |
|
| 96 |
+
# Cleanse and parse current incoming user message
|
| 97 |
user_content_str = extract_text(message)
|
| 98 |
messages.append({"role": "user", "content": user_content_str})
|
| 99 |
|
| 100 |
response = ""
|
| 101 |
|
| 102 |
+
# Stream responses back reliably from the new model API
|
| 103 |
try:
|
| 104 |
for message_chunk in client.chat_completion(
|
| 105 |
messages,
|