File size: 1,323 Bytes
2fb4096
 
 
 
 
 
 
a8360a2
2fb4096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import os

# Load API URL and token from environment variables
API_URL = os.getenv("HF_API_URL", "https://api-inference.huggingface.co/models/your-model")
API_TOKEN = os.getenv("HF_API_TOKEN", "your-default-token")  # Replace with your actual token for fallback
print(API_URL)
# Function to call the Hugging Face Inference API
def call_huggingface_api(input_text):
    headers = {"Authorization": f"Bearer {API_TOKEN}"}
    payload = {"inputs": input_text}
    try:
        response = requests.post(API_URL, headers=headers, json=payload)
        if response.status_code == 200:
            data = response.json()
            return f"Question: {input_text}\nAnswer: {data.get('answer', 'No answer found.')}\nConfidence: {data.get('confidence', 'N/A')}"
        else:
            return f"Error: {response.status_code} - {response.text}"
    except Exception as e:
        return f"Error during API call: {e}"

# Gradio Interface
gr.Interface(
    fn=call_huggingface_api,
    inputs="text",
    outputs="text",
    examples=[
        ["Who is rahul7star?"],
        ["What does Rahul7star do?"],
        ["Tell me about Rahul7star"]
    ],
    title="Ask Rahul7star AI",
    description="Ask questions about rahul7star and get answers powered by Hugging Face Inference API."
).launch()