srinuksv commited on
Commit
4602937
1 Parent(s): 90f8a56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -31
app.py CHANGED
@@ -1,9 +1,9 @@
 
1
  import gradio as gr
 
2
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
3
- from llama_index.core import ChatPromptTemplate, Settings, StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader
4
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
5
- from dotenv import load_dotenv
6
- import os
7
 
8
  # Load environment variables
9
  load_dotenv()
@@ -82,34 +82,27 @@ def handle_query(query):
82
  print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
83
  data_ingestion_from_directory()
84
 
85
- def predict(message, history):
86
- messages = [{"role": "system", "content": "You are a helpful assistant."}]
87
- for user_message, bot_message in history:
88
- if user_message:
89
- messages.append({"role": "user", "content": user_message})
90
- if bot_message:
91
- messages.append({"role": "assistant", "content": bot_message})
92
- messages.append({"role": "user", "content": message})
93
-
94
- response = ""
95
- for chunk in Settings.llm.create_chat_completion(
96
- stream=True,
97
- messages=messages,
98
- ):
99
- part = chunk["choices"][0]["delta"].get("content", None)
100
- if part:
101
- response += part
102
- yield response
103
-
104
- # Create a Gradio chat interface
105
- demo = gr.Interface(
106
- fn=predict,
107
- inputs=gr.Textbox(label="User Input", placeholder="Type your message here..."),
108
- outputs=gr.Textbox(label="Bot Response", placeholder="Bot's response will appear here..."),
109
- title="RedFernsTech Chatbot",
110
- theme="compact",
111
- live=True # Enables real-time updates
112
  )
113
 
114
  # Launch the Gradio interface
115
- demo.launch()
 
1
+ from dotenv import load_dotenv
2
  import gradio as gr
3
+ from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
4
  from llama_index.llms.huggingface import HuggingFaceInferenceAPI
 
5
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
6
+ from sentence_transformers import SentenceTransformer
 
7
 
8
  # Load environment variables
9
  load_dotenv()
 
82
  print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
83
  data_ingestion_from_directory()
84
 
85
+ # Define the input and output components for the Gradio interface
86
+ input_component = gr.Textbox(
87
+ show_label=False,
88
+ placeholder="Ask me anything about the document..."
89
+ )
90
+
91
+ output_component = gr.Textbox()
92
+
93
+ # Function to handle queries
94
+ def chatbot_handler(query):
95
+ response = handle_query(query)
96
+ return response
97
+
98
+ # Create the Gradio interface
99
+ interface = gr.Interface(
100
+ fn=chatbot_handler,
101
+ inputs=input_component,
102
+ outputs=output_component,
103
+ title="RedfernsTech Q&A Chatbot",
104
+ description="Ask me anything about the uploaded document."
 
 
 
 
 
 
 
105
  )
106
 
107
  # Launch the Gradio interface
108
+ interface.launch(share=True)