Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Load API URL and token from environment variables
|
| 6 |
+
API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/your-model")
|
| 7 |
+
API_TOKEN = os.getenv("HF_API_TOKEN", "your-default-token") # Replace with your actual token for fallback
|
| 8 |
+
|
| 9 |
+
# Function to call the Hugging Face Inference API
|
| 10 |
+
def call_huggingface_api(input_text):
|
| 11 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 12 |
+
payload = {"inputs": input_text}
|
| 13 |
+
try:
|
| 14 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 15 |
+
if response.status_code == 200:
|
| 16 |
+
data = response.json()
|
| 17 |
+
return f"Question: {input_text}\nAnswer: {data.get('answer', 'No answer found.')}\nConfidence: {data.get('confidence', 'N/A')}"
|
| 18 |
+
else:
|
| 19 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error during API call: {e}"
|
| 22 |
+
|
| 23 |
+
# Gradio Interface
|
| 24 |
+
gr.Interface(
|
| 25 |
+
fn=call_huggingface_api,
|
| 26 |
+
inputs="text",
|
| 27 |
+
outputs="text",
|
| 28 |
+
examples=[
|
| 29 |
+
["Who is rahul7star?"],
|
| 30 |
+
["What does Rahul7star do?"],
|
| 31 |
+
["Tell me about Rahul7star"]
|
| 32 |
+
],
|
| 33 |
+
title="Ask Rahul7star AI",
|
| 34 |
+
description="Ask questions about rahul7star and get answers powered by Hugging Face Inference API."
|
| 35 |
+
).launch()
|