kshahnathwani commited on
Commit
b962ca0
·
verified ·
1 Parent(s): 972e191

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
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 (in Space secrets)
50
  HF_TOKEN = os.environ.get("HF_TOKEN")
51
 
52
- # Inference client
53
- client = InferenceClient(token=HF_TOKEN)
 
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 a simple prompt
61
  prompt = f"{system_message}\nUser: {message}\nAnswer:"
62
 
63
- # Raw POST to Hugging Face Inference API
64
- output = client.post_json(
65
- "https://api-inference.huggingface.co/models/gpt2", # ✅ change model here if needed
66
- {
67
- "inputs": prompt,
68
- "parameters": {
69
- "max_new_tokens": max_tokens,
70
- "temperature": temperature,
71
- "top_p": top_p,
72
- },
73
  },
74
- )
75
 
76
- # Parse response
77
- if isinstance(output, list) and len(output) > 0 and "generated_text" in output[0]:
78
- response = output[0]["generated_text"]
79
- else:
80
- response = str(output)
81
 
82
- return response.strip()
 
 
 
 
 
 
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()