Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,37 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
)
|
7 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
|
6 |
+
load_dotenv()
|
7 |
+
HUGGINGFACE_TOKEN = os.getenv('HUGGINGFACE_TOKEN')
|
8 |
+
|
9 |
+
# Hugging Face API configuration
|
10 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
11 |
+
HEADERS = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
|
12 |
+
|
13 |
+
# Function to interact with the Hugging Face model
|
14 |
+
def query_huggingface_api(input_text):
|
15 |
+
payload = {"inputs": input_text}
|
16 |
+
try:
|
17 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload)
|
18 |
+
response.raise_for_status() # Raise error for HTTP errors
|
19 |
+
return response.json()[0]["generated_text"]
|
20 |
+
except requests.exceptions.RequestException as e:
|
21 |
+
return f"Error: {str(e)}"
|
22 |
+
|
23 |
+
# Gradio interface
|
24 |
+
def chatbot(input_text):
|
25 |
+
response = query_huggingface_api(input_text)
|
26 |
+
return response
|
27 |
+
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=chatbot,
|
30 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
|
31 |
+
outputs=gr.Textbox(),
|
32 |
+
title="AI Chatbot",
|
33 |
+
description="Chat with the AI powered by Hugging Face Mistral-7B-Instruct-v0.3.",
|
34 |
)
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
iface.launch()
|