File size: 2,020 Bytes
2c66489
 
8685580
 
2c66489
 
 
 
 
6f371eb
2c66489
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d631854
2c66489
 
 
 
 
 
 
 
 
 
 
 
8685580
2c66489
 
 
 
 
6f371eb
2c66489
6f371eb
2c66489
 
 
 
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
import gradio as gr
import requests
from huggingface_hub import InferenceClient

def query_huggingface(api_key, prompt):
    url = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
    headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
    data = {"inputs": prompt, "parameters": {"max_tokens": 500}}
    try:
        response = requests.post(url, headers=headers, json=data)
        response_json = response.json()
        return response_json[0]["generated_text"] if isinstance(response_json, list) else "Error: Unexpected response format"
    except Exception as e:
        return f"Error: {str(e)}"

def query_huggingfacehub(api_key, prompt):
    client = InferenceClient(token=api_key)
    try:
        completion = client.text_generation("mistralai/Mistral-7B-Instruct-v0.3", prompt, max_new_tokens=500)
        return completion if completion else "Error: No response"
    except Exception as e:
        return f"Error: {str(e)}"

def chatbot(api_key, user_input, advanced_reasoning, chat_history):
    if not api_key:
        return "Error: API Key is required.", chat_history
    
    if user_input:
        chat_history += f"You: {user_input}\n"
        if advanced_reasoning:
            ai_response = query_huggingfacehub(api_key, user_input)
        else:
            ai_response = query_huggingface(api_key, user_input)
        chat_history += f"VarunGPT: {ai_response}\n"
        return ai_response, chat_history
    return "", chat_history

def clear_chat():
    return ""

gr.Interface(
    fn=chatbot,
    inputs=[gr.Textbox(label="Enter API Key", type="password"),
            gr.Textbox(label="Ask anything..."),
            gr.Checkbox(label="Enable Advanced Reasoning"),
            gr.State("")],
    outputs=[gr.Textbox(label="VarunGPT Response"),
             gr.State("")],
    title="🤖 VarunGPT - AI Chatbot",
    description="A simple AI chatbot powered by Hugging Face models with chat history.",
    live=True,
).launch()