JeoManojThomas commited on
Commit
bdfc7f5
·
verified ·
1 Parent(s): a04c561

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -24
app.py CHANGED
@@ -1,33 +1,22 @@
1
  import gradio as ui
2
- import requests
3
- import spaces # Import the Hugging Face ZeroGPU spaces module
4
 
5
- # Using a reliable, free, open-source model endpoint
6
- API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
 
7
 
8
- # The @spaces.GPU decorator satisfies the ZeroGPU startup health check
9
  @spaces.GPU
10
  def generate_text(prompt):
11
- payload = {
12
- "inputs": prompt,
13
- "parameters": {
14
- "max_new_tokens": 250,
15
- "temperature": 0.7,
16
- "return_full_text": False
17
- }
18
- }
19
-
20
  try:
21
- response = requests.post(API_URL, json=payload)
22
- output = response.json()
23
-
24
- # Parse the response safely
25
- if isinstance(output, list) and len(output) > 0:
26
- return output[0].get('generated_text', 'No text generated.')
27
- elif isinstance(output, dict) and 'error' in output:
28
- return f"API Error: {output['error']}"
29
- else:
30
- return "Unexpected response format."
31
 
32
  except Exception as e:
33
  return f"An error occurred: {str(e)}"
 
1
  import gradio as ui
2
+ import spaces
3
+ from huggingface_hub import InferenceClient
4
 
5
+ # Initialize the official Hugging Face client
6
+ # Inside a Space, this automatically authenticates and routes requests correctly
7
+ client = InferenceClient()
8
 
 
9
  @spaces.GPU
10
  def generate_text(prompt):
 
 
 
 
 
 
 
 
 
11
  try:
12
+ # We use the client to chat with the model instead of making direct HTTP requests
13
+ response = client.text_generation(
14
+ model="HuggingFaceH4/zephyr-7b-beta",
15
+ prompt=prompt,
16
+ max_new_tokens=250,
17
+ temperature=0.7
18
+ )
19
+ return response if response else "No text generated."
 
 
20
 
21
  except Exception as e:
22
  return f"An error occurred: {str(e)}"