ariankhalfani commited on
Commit
9874be5
1 Parent(s): 01d7a69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -7,14 +7,20 @@ import numpy as np
7
  from sentence_transformers import SentenceTransformer
8
  import gradio as gr
9
 
10
- # Configure Hugging Face API
11
- huggingface_api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct"
 
 
 
 
 
 
12
  huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
13
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
14
 
15
  # Function to query Hugging Face model
16
- def query_huggingface(payload):
17
- response = requests.post(huggingface_api_url, headers=headers, json=payload)
18
  return response.json()
19
 
20
  # Function to extract text from PDF
@@ -83,12 +89,12 @@ init_db()
83
  model = SentenceTransformer('all-MiniLM-L6-v2')
84
  faiss_index, context_list = update_faiss_index()
85
 
86
- # Gradio interface
87
- def chatbot(question):
88
  relevant_contexts = retrieve_relevant_context(faiss_index, context_list, question)
89
  user_input = f"question: {question} context: {' '.join(relevant_contexts)}"
90
- response = query_huggingface({"inputs": user_input})
91
- response_text = response.get("generated_text", "Sorry, I couldn't generate a response.")
92
  return response_text
93
 
94
  # File upload function
@@ -100,8 +106,13 @@ def upload_pdf(file):
100
  return "PDF content added to context."
101
 
102
  # Gradio interface
103
- iface = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Storage Warehouse Customer Service Chatbot")
 
 
 
 
 
104
  file_upload = gr.Interface(fn=upload_pdf, inputs="file", outputs="text", title="Upload PDF for Context")
105
 
106
  app = gr.TabbedInterface([iface, file_upload], ["Chatbot", "Upload PDF"])
107
- app.launch()
 
7
  from sentence_transformers import SentenceTransformer
8
  import gradio as gr
9
 
10
+ # Configure Hugging Face API URLs and headers
11
+ api_urls = {
12
+ "Meta-Llama-3-70B-Instruct": "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B-Instruct",
13
+ "Meta-Llama-3-8B-Instruct": "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct",
14
+ "Gemma-2-27B-IT": "https://api-inference.huggingface.co/models/google/gemma-2-27b-it",
15
+ "Gemma-2-27B": "https://api-inference.huggingface.co/models/google/gemma-2-27b"
16
+ }
17
+
18
  huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
19
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
20
 
21
  # Function to query Hugging Face model
22
+ def query_huggingface(model_name, payload):
23
+ response = requests.post(api_urls[model_name], headers=headers, json=payload)
24
  return response.json()
25
 
26
  # Function to extract text from PDF
 
89
  model = SentenceTransformer('all-MiniLM-L6-v2')
90
  faiss_index, context_list = update_faiss_index()
91
 
92
+ # Gradio interface for chatbot
93
+ def chatbot(model_name, question):
94
  relevant_contexts = retrieve_relevant_context(faiss_index, context_list, question)
95
  user_input = f"question: {question} context: {' '.join(relevant_contexts)}"
96
+ response = query_huggingface(model_name, {"inputs": user_input})
97
+ response_text = response[0].get("generated_text", "Sorry, I couldn't generate a response.") if isinstance(response, list) else response.get("generated_text", "Sorry, I couldn't generate a response.")
98
  return response_text
99
 
100
  # File upload function
 
106
  return "PDF content added to context."
107
 
108
  # Gradio interface
109
+ iface = gr.Interface(
110
+ fn=chatbot,
111
+ inputs=[gr.inputs.Dropdown(["Meta-Llama-3-70B-Instruct", "Meta-Llama-3-8B-Instruct", "Gemma-2-27B-IT", "Gemma-2-27B"]), "text"],
112
+ outputs="text",
113
+ title="Storage Warehouse Customer Service Chatbot"
114
+ )
115
  file_upload = gr.Interface(fn=upload_pdf, inputs="file", outputs="text", title="Upload PDF for Context")
116
 
117
  app = gr.TabbedInterface([iface, file_upload], ["Chatbot", "Upload PDF"])
118
+ app.launch(share=True)