Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
import traceback
|
|
|
|
| 5 |
|
| 6 |
# Styling
|
| 7 |
fancy_css = """
|
|
@@ -46,40 +46,44 @@ Given a list of notes (like "C E G" or "D F# A C"), identify the chord name.
|
|
| 46 |
Always respond with the chord name and a short explanation of the intervals.
|
| 47 |
"""
|
| 48 |
|
| 49 |
-
# Hugging Face API token (
|
| 50 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 56 |
if HF_TOKEN is None:
|
| 57 |
return "⚠️ No HF_TOKEN found. Please add it in your Space secrets."
|
| 58 |
|
| 59 |
try:
|
| 60 |
-
# Build
|
| 61 |
prompt = f"{system_message}\nUser: {message}\nAnswer:"
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"
|
| 66 |
-
{
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
|
| 70 |
-
"temperature": temperature,
|
| 71 |
-
"top_p": top_p,
|
| 72 |
-
},
|
| 73 |
},
|
| 74 |
-
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
response
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
tb = traceback.format_exc()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import traceback
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
# Styling
|
| 7 |
fancy_css = """
|
|
|
|
| 46 |
Always respond with the chord name and a short explanation of the intervals.
|
| 47 |
"""
|
| 48 |
|
| 49 |
+
# Hugging Face API token (from Space secrets)
|
| 50 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 51 |
|
| 52 |
+
# API URL – using GPT-2 as demo, can be swapped for another model
|
| 53 |
+
API_URL = "https://api-inference.huggingface.co/models/gpt2"
|
| 54 |
+
|
| 55 |
|
| 56 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 57 |
if HF_TOKEN is None:
|
| 58 |
return "⚠️ No HF_TOKEN found. Please add it in your Space secrets."
|
| 59 |
|
| 60 |
try:
|
| 61 |
+
# Build prompt
|
| 62 |
prompt = f"{system_message}\nUser: {message}\nAnswer:"
|
| 63 |
|
| 64 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 65 |
+
payload = {
|
| 66 |
+
"inputs": prompt,
|
| 67 |
+
"parameters": {
|
| 68 |
+
"max_new_tokens": max_tokens,
|
| 69 |
+
"temperature": temperature,
|
| 70 |
+
"top_p": top_p,
|
|
|
|
|
|
|
|
|
|
| 71 |
},
|
| 72 |
+
}
|
| 73 |
|
| 74 |
+
# Call Hugging Face Inference API
|
| 75 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 76 |
+
|
| 77 |
+
if response.status_code != 200:
|
| 78 |
+
return f"⚠️ API Error {response.status_code}: {response.text}"
|
| 79 |
|
| 80 |
+
data = response.json()
|
| 81 |
+
|
| 82 |
+
# Extract generated text
|
| 83 |
+
if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
|
| 84 |
+
return data[0]["generated_text"].strip()
|
| 85 |
+
else:
|
| 86 |
+
return str(data)
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
tb = traceback.format_exc()
|