AK97GAMERZ commited on
Commit
44dc050
·
verified ·
1 Parent(s): 4b49fd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
app.py CHANGED
@@ -2,22 +2,29 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- # Set your Hugging Face API key
6
  API_KEY = os.getenv("HF_API_KEY")
7
 
8
- # Hugging Face Model Endpoint (Change to your preferred LLM)
9
  MODEL_NAME = "mistralai/Mistral-7B-Instruct"
 
10
 
11
- # Function to get response from Hugging Face LLM
12
  def chat_with_llm(prompt):
 
 
 
 
13
  headers = {"Authorization": f"Bearer {API_KEY}"}
14
  payload = {"inputs": prompt}
15
- response = requests.post(
16
- f"https://api-inference.huggingface.co/models/{MODEL_NAME}",
17
- headers=headers,
18
- json=payload
19
- )
20
- return response.json()[0]["generated_text"]
 
 
 
21
 
22
  # Create Gradio Chat UI
23
  iface = gr.Interface(
@@ -28,5 +35,5 @@ iface = gr.Interface(
28
  description="A free AI chatbot using Hugging Face's API. Supports multiple LLMs!",
29
  )
30
 
31
- # Launch App
32
- iface.launch()
 
2
  import requests
3
  import os
4
 
5
+ # Fetch Hugging Face API key from environment variables
6
  API_KEY = os.getenv("HF_API_KEY")
7
 
8
+ # Define the Hugging Face model endpoint
9
  MODEL_NAME = "mistralai/Mistral-7B-Instruct"
10
+ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
11
 
 
12
  def chat_with_llm(prompt):
13
+ """Sends a prompt to the Hugging Face model and returns the response."""
14
+ if not API_KEY:
15
+ return "Error: API key is missing. Please set 'HF_API_KEY' in your environment variables."
16
+
17
  headers = {"Authorization": f"Bearer {API_KEY}"}
18
  payload = {"inputs": prompt}
19
+
20
+ try:
21
+ response = requests.post(API_URL, headers=headers, json=payload)
22
+ response.raise_for_status() # Raise error for HTTP issues
23
+ result = response.json()
24
+ return result[0]["generated_text"] if result else "No response from model."
25
+
26
+ except requests.exceptions.RequestException as e:
27
+ return f"API Error: {str(e)}"
28
 
29
  # Create Gradio Chat UI
30
  iface = gr.Interface(
 
35
  description="A free AI chatbot using Hugging Face's API. Supports multiple LLMs!",
36
  )
37
 
38
+ # Launch the app
39
+ iface.launch()