File size: 1,510 Bytes
2fb4096
 
 
 
 
 
5761548
cda1e78
2fb4096
 
 
 
 
 
cda1e78
 
 
2fb4096
 
 
 
cda1e78
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
37
38
39
40
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 ackktual token for fallback

# 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)
        print(f"Request sent to: {API_URL}")
        print(f"Payload: {payload}")
        print(f"Response Status Code: {response.status_code}")
        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:
            print(f"Error Response: {response.text}")
            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()