Spaces:
Sleeping
Sleeping
File size: 4,514 Bytes
cfcf9e6 39d67a5 3b1501a 87524cd 93076d0 87524cd 8cc3a3b 87524cd 93076d0 39d67a5 87524cd 8cc3a3b 87524cd 39d67a5 87524cd 39d67a5 87524cd 93076d0 87524cd 8cc3a3b 93076d0 39d67a5 87524cd 39d67a5 87524cd 8cc3a3b 87524cd 93076d0 39d67a5 3b1501a 93076d0 87524cd 8cc3a3b 93076d0 87524cd 8cc3a3b bbb4028 87524cd bbb4028 87524cd 93076d0 bbb4028 93076d0 8cc3a3b cfcf9e6 87524cd 39d67a5 93076d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import gradio as gr
import os
import requests
# --------------------------
# Configuration
# --------------------------
# ✅ Your actual Inference Endpoint URL
HF_ENDPOINT_URL = "https://dqptyla9qxd15452.us-east-1.aws.endpoints.huggingface.cloud"
# ✅ Your token securely accessed via Hugging Face Secrets
HF_TOKEN = os.environ.get("HF_TOKEN")
headers = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json"
}
# --------------------------
# Function to Call Endpoint
# --------------------------
def generate_text(prompt, max_length=150, temperature=0.7):
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": max_length,
"temperature": temperature,
"top_k": 50,
"top_p": 0.95,
"repetition_penalty": 1.5
}
}
try:
response = requests.post(HF_ENDPOINT_URL, headers=headers, json=payload)
if response.status_code == 200:
return response.json()[0]["generated_text"]
elif response.status_code == 404:
return "❌ Endpoint not found. Please check your HF_ENDPOINT_URL."
else:
return f"❌ Error {response.status_code}: {response.text}"
except Exception as e:
return f"❌ Failed to connect to endpoint: {str(e)}"
# --------------------------
# Gradio UI
# --------------------------
with gr.Blocks(title="Burmese-GPT-v3 (Hosted Inference Endpoint)") as demo:
gr.Markdown("## 🧠 Burmese GPT-3 Text Generator")
gr.Markdown("This app connects to a Hugging Face Inference Endpoint to generate Burmese language text.\n\nJust enter a prompt in Burmese, adjust settings, and hit 'Generate Text'!")
with gr.Row():
with gr.Column(scale=3):
prompt = gr.Textbox(
lines=5,
placeholder="Enter your Burmese text here...",
label="Input Prompt"
)
with gr.Column(scale=1):
max_length = gr.Slider(
minimum=50,
maximum=500,
value=150,
step=10,
label="Max New Tokens"
)
temperature = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.7,
step=0.1,
label="Temperature"
)
generate_btn = gr.Button("🚀 Generate Text")
output = gr.Textbox(lines=10, label="Generated Output")
generate_btn.click(
fn=generate_text,
inputs=[prompt, max_length, temperature],
outputs=output
)
with gr.Accordion("📌 Example Prompts", open=False):
gr.Markdown("Click on a prompt below to try it out:")
examples = [
["မင်္ဂလာပါ။ ကျွန်တော်က ကိုအောင်ပါ။ ရန်ကုန်မှာနေတယ်။ ဆရာလုပ်ပါတယ်။", 150, 0.7],
["မြန်မာနိုင်ငံမှာ မိုးရာသီမှာ မိုးဘယ်လောက်ကျလဲ။", 150, 0.6],
["အောင်မြင်တဲ့ကျောင်းသူတစ်ယောက်ဖြစ်ဖို့ ဘယ်လိုလေ့ကျင့်ရမလဲ။", 200, 0.8],
["မြန်မာ့ယဉ်ကျေးမှုအကြောင်း ပြောပြပါ။", 150, 0.7],
["သင်တောင်ကြီးသွားဖူးလား။ ဘယ်မှာသွားဖူးလဲ။", 150, 0.6]
]
for idx, ex in enumerate(examples):
example_btn = gr.Button(f"Example {idx+1}")
example_btn.click(
lambda e=ex: (e[0], e[1], e[2]),
inputs=[],
outputs=[prompt, max_length, temperature]
).then(
fn=generate_text,
inputs=[prompt, max_length, temperature],
outputs=output
)
gr.Markdown("""
---
### ℹ️ Troubleshooting
- If the app says "❌ Endpoint not found", double-check your `HF_ENDPOINT_URL`.
- If the model takes a few seconds to respond, it's just warming up from idle.
- Ensure your Hugging Face token is added as `HF_TOKEN` under **Settings → Secrets**.
""")
# --------------------------
# Launch
# --------------------------
demo.launch(
show_error=True,
server_name="0.0.0.0",
share=False
)
|