Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def analyze_shampoo(prompt):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Create the Gradio interface
|
| 27 |
interface = gr.Interface(
|
|
@@ -29,7 +50,7 @@ interface = gr.Interface(
|
|
| 29 |
inputs=gr.Textbox(lines=10, placeholder="Enter your analysis prompt here..."),
|
| 30 |
outputs=gr.Textbox(lines=20),
|
| 31 |
title="Phi-3 Shampoo Analyzer",
|
| 32 |
-
description="Analyze shampoo ingredients based on user profiles"
|
| 33 |
)
|
| 34 |
|
| 35 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Set your API key as an environment variable
|
| 7 |
+
# Important: In the Hugging Face Space, set this as a secret
|
| 8 |
+
HF_API_KEY = os.environ.get("HF_API_KEY", "")
|
|
|
|
| 9 |
|
| 10 |
def analyze_shampoo(prompt):
|
| 11 |
+
"""
|
| 12 |
+
Send the prompt to the Hugging Face Inference API instead of loading the model locally
|
| 13 |
+
"""
|
| 14 |
+
API_URL = "https://api-inference.huggingface.co/models/microsoft/phi-3-mini-4k-instruct"
|
| 15 |
+
headers = {
|
| 16 |
+
"Authorization": f"Bearer {HF_API_KEY}",
|
| 17 |
+
"Content-Type": "application/json"
|
| 18 |
+
}
|
|
|
|
| 19 |
|
| 20 |
+
payload = {
|
| 21 |
+
"inputs": prompt,
|
| 22 |
+
"parameters": {
|
| 23 |
+
"max_new_tokens": 2048,
|
| 24 |
+
"temperature": 0.7,
|
| 25 |
+
"top_p": 0.95,
|
| 26 |
+
"return_full_text": False
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
if not HF_API_KEY:
|
| 32 |
+
return "Error: No API key provided. Please add the HF_API_KEY secret to your Space."
|
| 33 |
+
|
| 34 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 35 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 36 |
+
|
| 37 |
+
result = response.json()
|
| 38 |
+
if isinstance(result, list) and len(result) > 0:
|
| 39 |
+
# Extract the generated text
|
| 40 |
+
return result[0].get('generated_text', 'No response generated')
|
| 41 |
+
else:
|
| 42 |
+
return f"Unexpected response format: {result}"
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error: {str(e)}"
|
| 46 |
|
| 47 |
# Create the Gradio interface
|
| 48 |
interface = gr.Interface(
|
|
|
|
| 50 |
inputs=gr.Textbox(lines=10, placeholder="Enter your analysis prompt here..."),
|
| 51 |
outputs=gr.Textbox(lines=20),
|
| 52 |
title="Phi-3 Shampoo Analyzer",
|
| 53 |
+
description="Analyze shampoo ingredients based on user profiles using the Hugging Face Inference API"
|
| 54 |
)
|
| 55 |
|
| 56 |
interface.launch()
|