Spaces:
Sleeping
Sleeping
updata2 app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
HEADERS = {"Authorization": "Bearer HF_API_KEY"}
|
| 7 |
|
| 8 |
def ask_model(prompt):
|
|
|
|
|
|
|
|
|
|
| 9 |
response = requests.post(
|
| 10 |
-
|
| 11 |
-
headers=
|
| 12 |
-
json=
|
| 13 |
-
"model": MODEL_ID,
|
| 14 |
-
"inputs": prompt,
|
| 15 |
-
"parameters": {"max_new_tokens": 200},
|
| 16 |
-
},
|
| 17 |
)
|
|
|
|
| 18 |
if response.status_code == 200:
|
| 19 |
result = response.json()
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
else:
|
| 22 |
-
return f"Error {response.status_code}
|
| 23 |
|
|
|
|
| 24 |
iface = gr.Interface(
|
| 25 |
fn=ask_model,
|
| 26 |
-
inputs=
|
| 27 |
outputs="text",
|
| 28 |
title="Flutter AI Proxy",
|
| 29 |
description="Flutter uygulaman buraya istek atacak, model yanıt dönecek."
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Token environment variable olarak alınır (Settings > Variables'da tanımlandı)
|
| 6 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
|
|
|
| 7 |
|
| 8 |
def ask_model(prompt):
|
| 9 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 10 |
+
data = {"inputs": prompt}
|
| 11 |
+
|
| 12 |
response = requests.post(
|
| 13 |
+
"https://router.huggingface.co/hf-inference/models/mistralai/Mistral-7B-Instruct-v0.2",
|
| 14 |
+
headers=headers,
|
| 15 |
+
json=data
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
+
|
| 18 |
if response.status_code == 200:
|
| 19 |
result = response.json()
|
| 20 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
| 21 |
+
return result[0]["generated_text"]
|
| 22 |
+
else:
|
| 23 |
+
return str(result)
|
| 24 |
else:
|
| 25 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 26 |
|
| 27 |
+
# Gradio arayüzü
|
| 28 |
iface = gr.Interface(
|
| 29 |
fn=ask_model,
|
| 30 |
+
inputs="text",
|
| 31 |
outputs="text",
|
| 32 |
title="Flutter AI Proxy",
|
| 33 |
description="Flutter uygulaman buraya istek atacak, model yanıt dönecek."
|