DivarGPT / app.py
mohsenfayyaz's picture
Create app.py
4ac703a
raw
history blame
5.68 kB
import os
import openai
import gradio as gr
import random
import time
import json
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
# print(str(response.choices[0].message))
return response.choices[0].message["content"]
def collect_messages(prompt, context):
context.append({'role':'user', 'content':f"{prompt}"})
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
return response, context
DIVAR_CONTEXT = """
فرض کن ربات دستیار فروش برای اپلیکیشن دیوار هستی که کاربر را کمک کنی. در هر مرحله باید از کاربر سوالاتی بپرسی که نیاز کاربر مشخص شود و در نهایت محصول نهایی را پیشنهاد دهی. در هر مرحله باید از کاربر سوالی بپرسی و فقط آخر مکالمه یک کالای مشخص را پیشنهاد دهی.
اپلیکیشن و سایت دیوار، بستری برای انتشار رایگان انواع آگهی و نیازمندی خرید و فروش کالاهای نو و دست‌دوم است.
شما می توانید از اپلیکیشن دیوار برای دریافت و ارائه خدمات و همچنین، خرید و فروش کالا در دسته‌بندی‌‌های مختلف زیر استفاده کنید:
املاک: خرید و فروش خانه، آپارتمان، ویلا و مغازه
وسایل نقلیه : خرید و فروش خودروی سواری، کلاسیک و سنگین و آگهی‌های خودروهای اجاره‌ای
لوازم الکترونیکی : خرید و فروش موبایل و تبلت، لپ‌تاپ و رایانه، کنسول بازی، لوازم صوتی و تصویری و ...
وسایل منزل : خرید و فروش وسایل خانه، لوازم آشپزخانه، ابزار و وسایل ساختمان و ...
خدمات : انواع خدمات پذیرایی و مراسم، نظافت، سرگرمی، رایانه‌ای، مالی و حسابداری و ...
وسایل شخصی : خرید و فروش کیف، کفش، لباس، آرایشی و بهداشتی، لباس بچه، لوازم التحریر و ...
سرگرمی و فراغت : خرید و فروش بلیط و تور، کتاب و مجله، کلکسیون، آلات موسیقی، لوازم ورزشی و ...
اجتماعی : رویداد، رویدادهای داوطلبانه، گم‌شده‌ها و ...
برای کسب‌وکار : خرید و فروش تجهیزات و ماشین‌آلات، عمده‌فروشی و ...
استخدام و کاریابی : اداری و مدیریتی، سرایداری، عمران و ساختمانی، رایانه و فناوری اطلاعات، مالی و حسابداری و ...
در هر جا که ربات نیاز به سرچ در دیوار داشتی می توانی فقط یک جیسون تولید کنی به شکل زیر تا نتیجه را از طرف سیستم بگیری و سپس جواب کاربر را بدهی.
{"query": "...", "price_range": [min, max]}
شما باید کوتاه و بسیار دوستانه پاسخ دهید.
"""
base_context = [ {'role':'system', 'content': DIVAR_CONTEXT} ] # accumulate messages
save = ""
context = base_context.copy()
def respond(message, chat_history, api_key):
global context, save
try:
openai.api_key = api_key
save = api_key
bot_message, context = collect_messages(message, context)
chat_history.append((message, bot_message))
messages = context.copy()
messages.append(
{'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)' },
)
response = get_completion_from_messages(messages, temperature=0)
# save = response
query = json.loads(response)["search_query"][0]
h = f"<iframe src='https://www.google.com/search?igu=1&q=site:divar.ir+{query}' width='100%' height='400' title='description'></iframe>"
except Exception as e:
h, response = "WRONG API KEY", str(e)
return "", chat_history, h, response
def clear_fn():
context = base_context.copy()
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=1):
html = gr.HTML("Results Will be shown here!")
htmlj = gr.HTML("Results Will be shown here!")
with gr.Column(scale=1):
api_key_box = gr.Textbox(label="OpenAI API Key", info="OpenAI API Key", lines=1, value="")
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
msg.submit(respond, [msg, chatbot, api_key_box], [msg, chatbot, html, htmlj])
clear.click(clear_fn, None, chatbot, queue=False)
demo.launch()