Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,14 @@ HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
|
| 8 |
|
| 9 |
def query_model(prompt, max_tokens=256, temperature=0.7):
|
| 10 |
"""Query the model using Hugging Face Inference API"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 13 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 14 |
|
| 15 |
payload = {
|
| 16 |
"inputs": prompt,
|
|
@@ -30,18 +35,26 @@ def query_model(prompt, max_tokens=256, temperature=0.7):
|
|
| 30 |
return result[0].get('generated_text', 'No generated text found')
|
| 31 |
else:
|
| 32 |
return f"Unexpected response format: {result}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
error_msg = response.json().get('error', 'Unknown error')
|
| 35 |
-
return f"API Error ({response.status_code}): {error_msg}"
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
-
return f"Request failed: {str(e)}"
|
| 39 |
|
| 40 |
# Create Gradio interface
|
| 41 |
-
with gr.Blocks(title="0dAI 8x7B Model Demo") as demo:
|
| 42 |
gr.Markdown("""
|
| 43 |
-
# 0dAI 8x7B Model Demo
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
""")
|
| 46 |
|
| 47 |
with gr.Row():
|
|
@@ -69,13 +82,13 @@ with gr.Blocks(title="0dAI 8x7B Model Demo") as demo:
|
|
| 69 |
label="Temperature"
|
| 70 |
)
|
| 71 |
|
| 72 |
-
generate_btn = gr.Button("Generate", variant="primary")
|
| 73 |
|
| 74 |
with gr.Column():
|
| 75 |
output = gr.Textbox(
|
| 76 |
label="Generated Text",
|
| 77 |
lines=8,
|
| 78 |
-
|
| 79 |
)
|
| 80 |
|
| 81 |
# Examples
|
|
@@ -84,11 +97,21 @@ with gr.Blocks(title="0dAI 8x7B Model Demo") as demo:
|
|
| 84 |
["Explain quantum computing in simple terms:"],
|
| 85 |
["Write a short story about a robot learning to paint:"],
|
| 86 |
["What are the benefits of renewable energy?"],
|
| 87 |
-
["How does machine learning work?"]
|
|
|
|
| 88 |
],
|
| 89 |
-
inputs=prompt_input
|
|
|
|
| 90 |
)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
generate_btn.click(
|
| 93 |
fn=query_model,
|
| 94 |
inputs=[prompt_input, max_tokens, temperature],
|
|
|
|
| 8 |
|
| 9 |
def query_model(prompt, max_tokens=256, temperature=0.7):
|
| 10 |
"""Query the model using Hugging Face Inference API"""
|
| 11 |
+
|
| 12 |
+
# Check if token is available
|
| 13 |
+
if not HF_TOKEN:
|
| 14 |
+
return "❌ Error: Hugging Face token not found. Please add HF_TOKEN in Space settings → Repository secrets."
|
| 15 |
+
|
| 16 |
try:
|
| 17 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 18 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 19 |
|
| 20 |
payload = {
|
| 21 |
"inputs": prompt,
|
|
|
|
| 35 |
return result[0].get('generated_text', 'No generated text found')
|
| 36 |
else:
|
| 37 |
return f"Unexpected response format: {result}"
|
| 38 |
+
elif response.status_code == 401:
|
| 39 |
+
return "❌ Error: Invalid Hugging Face token. Please check your HF_TOKEN in Space settings."
|
| 40 |
+
elif response.status_code == 503:
|
| 41 |
+
# Model is loading
|
| 42 |
+
return "🔄 Model is loading, please wait 20-30 seconds and try again..."
|
| 43 |
else:
|
| 44 |
error_msg = response.json().get('error', 'Unknown error')
|
| 45 |
+
return f"❌ API Error ({response.status_code}): {error_msg}"
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"❌ Request failed: {str(e)}"
|
| 49 |
|
| 50 |
# Create Gradio interface
|
| 51 |
+
with gr.Blocks(title="0dAI 8x7B Model Demo", theme=gr.themes.Soft()) as demo:
|
| 52 |
gr.Markdown("""
|
| 53 |
+
# 🤖 0dAI 8x7B Model Demo
|
| 54 |
+
|
| 55 |
+
This Space uses the Hugging Face Inference API to run the [0dAI/0dAI-8x7b-0761](https://huggingface.co/0dAI/0dAI-8x7b-0761) model.
|
| 56 |
+
|
| 57 |
+
**Note:** Make sure you've added your Hugging Face token in Space settings!
|
| 58 |
""")
|
| 59 |
|
| 60 |
with gr.Row():
|
|
|
|
| 82 |
label="Temperature"
|
| 83 |
)
|
| 84 |
|
| 85 |
+
generate_btn = gr.Button("🚀 Generate", variant="primary")
|
| 86 |
|
| 87 |
with gr.Column():
|
| 88 |
output = gr.Textbox(
|
| 89 |
label="Generated Text",
|
| 90 |
lines=8,
|
| 91 |
+
show_copy_button=True
|
| 92 |
)
|
| 93 |
|
| 94 |
# Examples
|
|
|
|
| 97 |
["Explain quantum computing in simple terms:"],
|
| 98 |
["Write a short story about a robot learning to paint:"],
|
| 99 |
["What are the benefits of renewable energy?"],
|
| 100 |
+
["How does machine learning work?"],
|
| 101 |
+
["Write a Python function to calculate fibonacci sequence:"]
|
| 102 |
],
|
| 103 |
+
inputs=prompt_input,
|
| 104 |
+
label="Click any example to try:"
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# Status information
|
| 108 |
+
gr.Markdown("""
|
| 109 |
+
### 🔧 Setup Instructions:
|
| 110 |
+
1. Get your Hugging Face token from [Settings → Tokens](https://huggingface.co/settings/tokens)
|
| 111 |
+
2. Add it to this Space in **Settings → Repository secrets** as `HF_TOKEN`
|
| 112 |
+
3. The model may take 20-30 seconds to load on first request
|
| 113 |
+
""")
|
| 114 |
+
|
| 115 |
generate_btn.click(
|
| 116 |
fn=query_model,
|
| 117 |
inputs=[prompt_input, max_tokens, temperature],
|