Srinivasulu kethanaboina commited on
Commit
cb8f565
1 Parent(s): 01b984a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -9
app.py CHANGED
@@ -3,10 +3,11 @@ import os
3
  from http.cookies import SimpleCookie
4
  from dotenv import load_dotenv
5
  from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
6
- from llama_index.llms.huggingface import HuggingFaceInferenceAPI # Updated import
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
8
- import random
9
  import datetime
 
 
10
 
11
  # Load environment variables
12
  load_dotenv()
@@ -90,9 +91,41 @@ def handle_query(query, cookies=None):
90
  return response
91
 
92
  # Define the button click function
93
- def retrieve_history_and_redirect():
94
- # Directly save the chat history without displaying any message
95
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  # Define your Gradio chat interface function
98
  def chat_interface(message, history):
@@ -135,8 +168,6 @@ label.svelte-1b6s6s {display: none}
135
  div.svelte-rk35yg {display: none;}
136
  div.svelte-1rjryqp{display: none;}
137
  div.progress-text.svelte-z7cif2.meta-text {display: none;}
138
-
139
-
140
  '''
141
 
142
  # Use Gradio Blocks to wrap components
@@ -147,10 +178,10 @@ with gr.Blocks(css=css) as demo:
147
  redirect_button = gr.Button("Retrieve History & Redirect")
148
 
149
  # Connect the button with the function, and handle the redirection
150
- redirect_button.click(fn=retrieve_history_and_redirect)
151
 
152
  # Add a JavaScript function to handle redirection after the Gradio event is processed
153
- redirect_button.click(fn=None,js="() => { window.open('https://redfernstech.com/chat-bot-test', '_blank'); }")
154
 
155
  # Launch the Gradio interface
156
  demo.launch()
 
3
  from http.cookies import SimpleCookie
4
  from dotenv import load_dotenv
5
  from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
6
+ from llama_index.llms.huggingface import HuggingFaceInferenceAPI
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
 
8
  import datetime
9
+ from gradio_client import Client
10
+ import requests
11
 
12
  # Load environment variables
13
  load_dotenv()
 
91
  return response
92
 
93
  # Define the button click function
94
+ def retrieve_history_and_redirect(cookies):
95
+ # Initialize the Gradio client
96
+ client = Client("vilarin/Llama-3.1-8B-Instruct")
97
+
98
+ # Retrieve and format chat history
99
+ history = cookies.get('chat_history', '[]')
100
+ history_list = eval(history)
101
+ history_str = "\n".join(
102
+ [f"User: {entry['query']}\nBot: {entry['response']}" for entry in history_list]
103
+ )
104
+
105
+ # Prepare the message
106
+ message = f"""
107
+ Chat history:
108
+ {history_str}
109
+ """
110
+
111
+ # Call the Gradio API
112
+ result = client.predict(
113
+ message=message,
114
+ system_prompt="Summarize the text and provide client interest in 30-40 words in bullet points.",
115
+ temperature=0.8,
116
+ max_new_tokens=1024,
117
+ top_p=1,
118
+ top_k=20,
119
+ penalty=1.2,
120
+ api_name="/chat"
121
+ )
122
+
123
+ # Print the result for debugging
124
+ print(result)
125
+
126
+ # Send the result to the URL
127
+ response = requests.post("https://redfernstech.com/api/receive_result", json={"result": result})
128
+ print(response.status_code, response.text)
129
 
130
  # Define your Gradio chat interface function
131
  def chat_interface(message, history):
 
168
  div.svelte-rk35yg {display: none;}
169
  div.svelte-1rjryqp{display: none;}
170
  div.progress-text.svelte-z7cif2.meta-text {display: none;}
 
 
171
  '''
172
 
173
  # Use Gradio Blocks to wrap components
 
178
  redirect_button = gr.Button("Retrieve History & Redirect")
179
 
180
  # Connect the button with the function, and handle the redirection
181
+ redirect_button.click(fn=retrieve_history_and_redirect, inputs=[gr.State()])
182
 
183
  # Add a JavaScript function to handle redirection after the Gradio event is processed
184
+ redirect_button.click(fn=None, js="() => { window.open('https://redfernstech.com/chat-bot-test', '_blank'); }")
185
 
186
  # Launch the Gradio interface
187
  demo.launch()