mohsenfayyaz commited on
Commit
a9d7973
1 Parent(s): eb1e8eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
4
  import random
5
  import time
6
  import json
 
7
 
8
  def get_completion(prompt, model="gpt-3.5-turbo"):
9
  messages = [{"role": "user", "content": prompt}]
@@ -47,11 +48,10 @@ DIVAR_CONTEXT = """
47
  """
48
 
49
  base_context = [ {'role':'system', 'content': DIVAR_CONTEXT} ] # accumulate messages
50
- save = ""
51
- context = base_context.copy()
52
 
53
- def respond(message, chat_history, api_key):
54
- global context, save
 
55
  try:
56
  openai.api_key = api_key
57
  save = api_key
@@ -60,19 +60,17 @@ def respond(message, chat_history, api_key):
60
 
61
  messages = context.copy()
62
  messages.append(
63
- {'role':'system', 'content':'create a json based on the previous conversation (Make sure to only output the json and nothing more). The fields should be 1) search_query (make it a list of possible queries which can find the best items for the user in Divar website) 2) price_range with min and max number In Tomans 3) possible_filters (keys must be in english)' },
64
  )
65
  response = get_completion_from_messages(messages, temperature=0)
66
- # save = response
67
  query = json.loads(response)["search_query"][0]
68
  h = f"<iframe src='https://www.google.com/search?igu=1&q=site:divar.ir+{query}' width='100%' height='400' title='description'></iframe>"
69
  except Exception as e:
70
  h, response = "WRONG API KEY", str(e)
71
- return "", chat_history, h, response
72
 
73
  def clear_fn():
74
- global context
75
- context = base_context.copy()
76
 
77
  with gr.Blocks() as demo:
78
  with gr.Row():
@@ -84,7 +82,9 @@ with gr.Blocks() as demo:
84
  chatbot = gr.Chatbot()
85
  msg = gr.Textbox(label="Chat")
86
  clear = gr.Button("Clear")
87
- msg.submit(respond, [msg, chatbot, api_key_box], [msg, chatbot, html, htmlj])
88
- clear.click(clear_fn, None, chatbot, queue=False)
 
 
89
 
90
  demo.launch()
 
4
  import random
5
  import time
6
  import json
7
+ import ast
8
 
9
  def get_completion(prompt, model="gpt-3.5-turbo"):
10
  messages = [{"role": "user", "content": prompt}]
 
48
  """
49
 
50
  base_context = [ {'role':'system', 'content': DIVAR_CONTEXT} ] # accumulate messages
 
 
51
 
52
+
53
+ def respond(message, chat_history, api_key, context_box):
54
+ context = ast.literal_eval(context_box)
55
  try:
56
  openai.api_key = api_key
57
  save = api_key
 
60
 
61
  messages = context.copy()
62
  messages.append(
63
+ {'role':'system', 'content':'create a short json based on the previous conversation (Make sure to only output the json and nothing more). The fields should be 1) search_query (make it a list of possible queries which can find the best items for the user in Divar website) 2) price_range with min and max number In Tomans 3) possible_filters (keys must be in english)' },
64
  )
65
  response = get_completion_from_messages(messages, temperature=0)
 
66
  query = json.loads(response)["search_query"][0]
67
  h = f"<iframe src='https://www.google.com/search?igu=1&q=site:divar.ir+{query}' width='100%' height='400' title='description'></iframe>"
68
  except Exception as e:
69
  h, response = "WRONG API KEY", str(e)
70
+ return "", chat_history, h, response, context
71
 
72
  def clear_fn():
73
+ return None, base_context.copy()
 
74
 
75
  with gr.Blocks() as demo:
76
  with gr.Row():
 
82
  chatbot = gr.Chatbot()
83
  msg = gr.Textbox(label="Chat")
84
  clear = gr.Button("Clear")
85
+ context_box = gr.Textbox(base_context.copy(), label="Context", visible=False)
86
+ msg.submit(respond, [msg, chatbot, api_key_box, context_box], [msg, chatbot, html, htmlj, context_box])
87
+ clear.click(clear_fn, None, [chatbot, context_box], queue=False)
88
+
89
 
90
  demo.launch()