File size: 1,416 Bytes
b768fad
 
 
 
 
ba9f85e
b768fad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests

# This uses a free Hugging Face API model (no credit card needed)
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
HEADERS = {"Authorization": "Bearer YOUR_HF_TOKEN"}  # You'll get this in step 2 below

def chat_with_ai(prompt, history):
    if not prompt.strip():
        return "", history
    
    # Prepare the prompt for the model
    full_prompt = f"<s>[INST] You are a helpful AI project assistant. Answer clearly and concisely. {prompt} [/INST]"
    
    try:
        response = requests.post(API_URL, headers=HEADERS, json={"inputs": full_prompt})
        result = response.json()
        reply = result[0]["generated_text"].split("[/INST]")[-1].strip()
    except Exception as e:
        reply = f"⚠️ Error: {str(e)}. Please check your token or internet connection."
    
    history.append((prompt, reply))
    return "", history

# Build the UI
with gr.Blocks(theme="soft") as demo:
    gr.Markdown("# 🤖 Hermus AI Project Assistant")
    gr.Markdown("Ask me to write code, plan projects, or debug anything.")
    
    chatbot = gr.Chatbot(height=400)
    msg = gr.Textbox(label="Your message", placeholder="Type your project question here...")
    clear = gr.Button("Clear chat")
    
    msg.submit(chat_with_ai, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: None, None, chatbot, queue=False)

demo.launch()