Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,22 @@
|
|
| 1 |
import gradio as ui
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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)}"
|