Spaces:
Sleeping
Sleeping
mohsenfayyaz
commited on
Commit
·
4ac703a
1
Parent(s):
e529f5a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
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}]
|
10 |
+
response = openai.ChatCompletion.create(
|
11 |
+
model=model,
|
12 |
+
messages=messages,
|
13 |
+
temperature=0, # this is the degree of randomness of the model's output
|
14 |
+
)
|
15 |
+
return response.choices[0].message["content"]
|
16 |
+
|
17 |
+
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
|
18 |
+
response = openai.ChatCompletion.create(
|
19 |
+
model=model,
|
20 |
+
messages=messages,
|
21 |
+
temperature=temperature, # this is the degree of randomness of the model's output
|
22 |
+
)
|
23 |
+
# print(str(response.choices[0].message))
|
24 |
+
return response.choices[0].message["content"]
|
25 |
+
|
26 |
+
def collect_messages(prompt, context):
|
27 |
+
context.append({'role':'user', 'content':f"{prompt}"})
|
28 |
+
response = get_completion_from_messages(context)
|
29 |
+
context.append({'role':'assistant', 'content':f"{response}"})
|
30 |
+
return response, context
|
31 |
+
|
32 |
+
DIVAR_CONTEXT = """
|
33 |
+
فرض کن ربات دستیار فروش برای اپلیکیشن دیوار هستی که کاربر را کمک کنی. در هر مرحله باید از کاربر سوالاتی بپرسی که نیاز کاربر مشخص شود و در نهایت محصول نهایی را پیشنهاد دهی. در هر مرحله باید از کاربر سوالی بپرسی و فقط آخر مکالمه یک کالای مشخص را پیشنهاد دهی.
|
34 |
+
اپلیکیشن و سایت دیوار، بستری برای انتشار رایگان انواع آگهی و نیازمندی خرید و فروش کالاهای نو و دستدوم است.
|
35 |
+
شما می توانید از اپلیکیشن دیوار برای دریافت و ارائه خدمات و همچنین، خرید و فروش کالا در دستهبندیهای مختلف زیر استفاده کنید:
|
36 |
+
املاک: خرید و فروش خانه، آپارتمان، ویلا و مغازه
|
37 |
+
وسایل نقلیه : خرید و فروش خودروی سواری، کلاسیک و سنگین و آگهیهای خودروهای اجارهای
|
38 |
+
لوازم الکترونیکی : خرید و فروش موبایل و تبلت، لپتاپ و رایانه، کنسول بازی، لوازم صوتی و تصویری و ...
|
39 |
+
وسایل منزل : خرید و فروش وسایل خانه، لوازم آشپزخانه، ابزار و وسایل ساختمان و ...
|
40 |
+
خدمات : انواع خدمات پذیرایی و مراسم، نظافت، سرگرمی، رایانهای، مالی و حسابداری و ...
|
41 |
+
وسایل شخصی : خرید و فروش کیف، کفش، لباس، آرایشی و بهداشتی، لباس بچه، لوازم التحریر و ...
|
42 |
+
سرگرمی و فراغت : خرید و فروش بلیط و تور، کتاب و مجله، کلکسیون، آلات موسیقی، لوازم ورزشی و ...
|
43 |
+
اجتماعی : رویداد، رویدادهای داوطلبانه، گمشدهها و ...
|
44 |
+
برای کسبوکار : خرید و فروش تجهیزات و ماشینآلات، عمدهفروشی و ...
|
45 |
+
استخدام و کاریابی : اداری و مدیریتی، سرایداری، عمران و ساختمانی، رایانه و فناوری اطلاعات، مالی و حسابداری و ...
|
46 |
+
در هر جا که ربات نیاز به سرچ در دیوار داشتی می توانی فقط یک جیسون تولید کنی به شکل زیر تا نتیجه را از طرف سیستم بگیری و سپس جواب کاربر را بدهی.
|
47 |
+
{"query": "...", "price_range": [min, max]}
|
48 |
+
شما باید کوتاه و بسیار دوستانه پاسخ دهید.
|
49 |
+
"""
|
50 |
+
|
51 |
+
base_context = [ {'role':'system', 'content': DIVAR_CONTEXT} ] # accumulate messages
|
52 |
+
save = ""
|
53 |
+
context = base_context.copy()
|
54 |
+
|
55 |
+
def respond(message, chat_history, api_key):
|
56 |
+
global context, save
|
57 |
+
try:
|
58 |
+
openai.api_key = api_key
|
59 |
+
save = api_key
|
60 |
+
bot_message, context = collect_messages(message, context)
|
61 |
+
chat_history.append((message, bot_message))
|
62 |
+
|
63 |
+
messages = context.copy()
|
64 |
+
messages.append(
|
65 |
+
{'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)' },
|
66 |
+
)
|
67 |
+
response = get_completion_from_messages(messages, temperature=0)
|
68 |
+
# save = response
|
69 |
+
query = json.loads(response)["search_query"][0]
|
70 |
+
h = f"<iframe src='https://www.google.com/search?igu=1&q=site:divar.ir+{query}' width='100%' height='400' title='description'></iframe>"
|
71 |
+
except Exception as e:
|
72 |
+
h, response = "WRONG API KEY", str(e)
|
73 |
+
return "", chat_history, h, response
|
74 |
+
|
75 |
+
def clear_fn():
|
76 |
+
context = base_context.copy()
|
77 |
+
|
78 |
+
with gr.Blocks() as demo:
|
79 |
+
with gr.Row():
|
80 |
+
with gr.Column(scale=1):
|
81 |
+
html = gr.HTML("Results Will be shown here!")
|
82 |
+
htmlj = gr.HTML("Results Will be shown here!")
|
83 |
+
with gr.Column(scale=1):
|
84 |
+
api_key_box = gr.Textbox(label="OpenAI API Key", info="OpenAI API Key", lines=1, value="")
|
85 |
+
chatbot = gr.Chatbot()
|
86 |
+
msg = gr.Textbox()
|
87 |
+
clear = gr.Button("Clear")
|
88 |
+
msg.submit(respond, [msg, chatbot, api_key_box], [msg, chatbot, html, htmlj])
|
89 |
+
clear.click(clear_fn, None, chatbot, queue=False)
|
90 |
+
|
91 |
+
demo.launch()
|