Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
4 |
-
|
5 |
-
# Define the Hugging Face API details
|
6 |
-
API_URL = "https://api-inference.huggingface.co/models/Huzaifa367/chat-summarizer"
|
7 |
-
API_TOKEN = os.getenv("AUTH_TOKEN")
|
8 |
-
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
9 |
-
|
10 |
-
def predict(message, history):
|
11 |
try:
|
12 |
-
# Send request to the Hugging Face API with only the current message
|
13 |
-
payload = {"inputs": message}
|
14 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
15 |
response.raise_for_status() # Raise exception for non-2xx status codes
|
16 |
-
|
17 |
-
# Extract summary text from the response
|
18 |
-
summary_text = response.json()["summary_text"]
|
19 |
except requests.exceptions.RequestException as e:
|
20 |
-
|
21 |
-
summary_text
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
return summary_text
|
24 |
|
25 |
# Launch Gradio chat interface with the predict function
|
26 |
-
gr.ChatInterface(
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import requests
|
4 |
+
def query_huggingface(payload):
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
try:
|
|
|
|
|
6 |
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
7 |
response.raise_for_status() # Raise exception for non-2xx status codes
|
8 |
+
return response.json()
|
|
|
|
|
9 |
except requests.exceptions.RequestException as e:
|
10 |
+
print(f"Error querying Hugging Face API: {e}")
|
11 |
+
return {"summary_text": f"Error querying Hugging Face API: {e}"}
|
12 |
+
|
13 |
+
def respond(user_message):
|
14 |
+
# Construct input text for summarization (only user message)
|
15 |
+
input_text = f"User: {user_message}"
|
16 |
+
|
17 |
+
# Query Hugging Face API for summarization
|
18 |
+
payload = {"inputs": input_text}
|
19 |
+
response = query_huggingface(payload)
|
20 |
+
|
21 |
+
# Extract summary text from the API response
|
22 |
+
summary_text = response.get("summary_text", "No response from Hugging Face API")
|
23 |
+
|
24 |
return summary_text
|
25 |
|
26 |
# Launch Gradio chat interface with the predict function
|
27 |
+
gr.ChatInterface(respond).launch()
|