Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,90 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import sqlite3
|
| 4 |
+
import openai
|
| 5 |
+
import pinecone
|
| 6 |
+
import chromadb
|
| 7 |
+
from PyPDF2 import PdfReader
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
import os
|
| 10 |
+
from google.colab import auth
|
| 11 |
+
from googleapiclient.discovery import build
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Initialize APIs
|
| 14 |
+
openai.api_key = "YOUR_OPENAI_API_KEY"
|
| 15 |
+
pinecone.init(api_key="YOUR_PINECONE_API_KEY", environment="us-west1-gcp")
|
| 16 |
+
db = chromadb.Client() # Initialize ChromaDB
|
| 17 |
+
|
| 18 |
+
# Set up Gradio interface
|
| 19 |
+
def initialize_models(model_choice):
|
| 20 |
+
if model_choice == 'OpenAI':
|
| 21 |
+
return {
|
| 22 |
+
'embedding': openai.Embedding.create,
|
| 23 |
+
'chat': openai.ChatCompletion.create
|
| 24 |
+
}
|
| 25 |
+
elif model_choice == 'HuggingFace':
|
| 26 |
+
embedding_model = pipeline('feature-extraction', model='bert-base-uncased')
|
| 27 |
+
chat_model = pipeline('conversational', model='facebook/blenderbot-400M-distill')
|
| 28 |
+
return {
|
| 29 |
+
'embedding': embedding_model,
|
| 30 |
+
'chat': chat_model
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
def fetch_pdf_from_drive(file_id):
|
| 34 |
+
auth.authenticate_user()
|
| 35 |
+
drive_service = build('drive', 'v3')
|
| 36 |
+
request = drive_service.files().get_media(fileId=file_id)
|
| 37 |
+
file = io.BytesIO(request.execute())
|
| 38 |
+
pdf_reader = PdfReader(file)
|
| 39 |
+
text = ""
|
| 40 |
+
for page in pdf_reader.pages:
|
| 41 |
+
text += page.extract_text()
|
| 42 |
+
return text
|
| 43 |
+
|
| 44 |
+
def query_db(query, db_type):
|
| 45 |
+
conn = sqlite3.connect('data.db')
|
| 46 |
+
cursor = conn.cursor()
|
| 47 |
+
if db_type == 'Pinecone':
|
| 48 |
+
index = pinecone.Index('your-index-name')
|
| 49 |
+
results = index.query(query)
|
| 50 |
+
return results
|
| 51 |
+
elif db_type == 'ChromaDB':
|
| 52 |
+
# Example of ChromaDB query - adapt as needed
|
| 53 |
+
results = db.query(query)
|
| 54 |
+
return results
|
| 55 |
+
conn.close()
|
| 56 |
+
|
| 57 |
+
def generate_response(model_choice, query, chat_history, db_type):
|
| 58 |
+
models = initialize_models(model_choice)
|
| 59 |
+
if model_choice == 'OpenAI':
|
| 60 |
+
response = models['chat'](model='gpt-3.5-turbo', messages=chat_history + [{'role': 'user', 'content': query}])
|
| 61 |
+
return response['choices'][0]['message']['content'], chat_history + [{'role': 'user', 'content': query}, {'role': 'assistant', 'content': response['choices'][0]['message']['content']}]
|
| 62 |
+
elif model_choice == 'HuggingFace':
|
| 63 |
+
response = models['chat'](query)
|
| 64 |
+
return response['generated_text'], chat_history + [{'role': 'user', 'content': query}, {'role': 'assistant', 'content': response['generated_text']}]
|
| 65 |
+
|
| 66 |
+
def process_input(model_choice, query, db_type, file_id):
|
| 67 |
+
if file_id:
|
| 68 |
+
pdf_text = fetch_pdf_from_drive(file_id)
|
| 69 |
+
query = f"{query} {pdf_text}"
|
| 70 |
+
response, updated_history = generate_response(model_choice, query, chat_history, db_type)
|
| 71 |
+
return response, updated_history
|
| 72 |
+
|
| 73 |
+
def gradio_interface():
|
| 74 |
+
with gr.Blocks() as demo:
|
| 75 |
+
with gr.Row():
|
| 76 |
+
model_choice = gr.Dropdown(['OpenAI', 'HuggingFace'], label='Model Choice', value='OpenAI')
|
| 77 |
+
db_type = gr.Dropdown(['ChromaDB', 'Pinecone'], label='Database Type', value='ChromaDB')
|
| 78 |
+
file_id = gr.Textbox(label='Google Drive File ID', placeholder='Enter Google Drive file ID (for PDFs)')
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
chat_history = gr.Chatbot()
|
| 82 |
+
query = gr.Textbox(label='Query')
|
| 83 |
+
submit_button = gr.Button('Submit')
|
| 84 |
+
|
| 85 |
+
submit_button.click(fn=process_input, inputs=[model_choice, query, db_type, file_id], outputs=[chat_history])
|
| 86 |
+
|
| 87 |
+
return demo
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
+
gradio_interface().launch()
|