Spaces:
Runtime error
Runtime error
import gradio as gr | |
from huggingface_hub import InferenceClient | |
import gradio as gr | |
import random | |
import os | |
checkpoint = "mistralai/Mixtral-8x7B-Instruct-v0.1" | |
client = InferenceClient(checkpoint) | |
def format_prompt(raw_Prompt): | |
better_prompt = f"[INST] {raw_Prompt} [/INST]" | |
return better_prompt | |
def findShops(target_platform, target_region, target_field, target_rangeFrom, target_rangeTo, target_crossSelling, target_similarArticles, target_amount, accessKey): | |
if accessKey != os.environ['ACCESS_TOKEN']: | |
return 'You are not authorized to use this software.' | |
if target_platform == 'All': | |
basePrompt = f""" | |
Search for {target_amount} Shops with a website. | |
""" | |
else: | |
basePrompt = f""" | |
Search for {target_amount} Shops that use the platform {target_platform}. | |
""" | |
regionPrompt = f""" | |
Make sure the recommended shops are located in {target_region}. | |
""" | |
rangePrompt = f""" | |
The websites of the recommended shops should have between {target_rangeFrom} and {target_rangeTo} monthly visitors.""" | |
crossSellingPrompt = """ | |
The recommended shops should be potential customers for a software that improves the cross-selling on their website.""" | |
similarArticlesPrompt = """ | |
The recommended shops should be potential customers for a software that can recommend similar products to the customers.""" | |
fieldPrompt = f""" | |
Focus on shops that have a focus on the topic'{target_field}'.""" | |
stylePrompt = """ | |
Your response must be a list in markdown format. | |
Each element of the list must follow these rules: | |
- The title for each element must be the name of the shop. | |
- The information coming after the title must each start in a new line. | |
- The first information for each element must be a link to the website. | |
- The next information must always be the number of monthly website visitors. | |
- The next information must always be a summary of what is sold in the shop. | |
""" | |
fullPrompt = basePrompt | |
fullPrompt += regionPrompt | |
fullPrompt += rangePrompt | |
if target_crossSelling: | |
fullPrompt += crossSellingPrompt | |
if target_similarArticles: | |
fullPrompt += similarArticlesPrompt | |
if target_field != 'All': | |
fullPrompt += fieldPrompt | |
fullPrompt += stylePrompt | |
generate_kwargs = dict( | |
temperature=0.2, | |
max_new_tokens=10000, | |
top_p=0.9, | |
repetition_penalty=1.0, | |
do_sample=True, | |
seed=random.randint(0, 100000), | |
) | |
stream = client.text_generation(format_prompt(fullPrompt), | |
**generate_kwargs, | |
stream=True, | |
details=True, | |
return_full_text=False) | |
output = "" | |
for response in stream: | |
output += response.token.text | |
output = output.removesuffix('</s>') | |
return output | |
with gr.Blocks(title="Shop finder", | |
theme=gr.themes.Soft(primary_hue="purple")) as demo: | |
gr.Markdown( | |
"## Shop finder") | |
gr.Markdown( | |
"Choose the right settings and click **FIND** to search for shops.") | |
with gr.Group(): | |
gr.Markdown("**FILTERS**") | |
with gr.Column(): | |
platform = gr.Dropdown(choices=['All', 'Shopware', 'Shopify'], | |
value='Shopware', | |
interactive=True, | |
label="PLATFORM") | |
region = gr.Dropdown(choices=['Germany', 'England', 'France', 'Italy', 'Austria'], | |
value='Germany', | |
interactive=True, | |
label="REGION") | |
field = gr.Dropdown(choices=['All', 'Fashion', 'Outdoor', 'Home & Living', | |
'Beauty & Cosmetics', 'Luggage & Bags', | |
'Office & School materials','Pets','Sports', | |
'Tech','Toys','Jewelry & Accessories','Food'], | |
value='All', | |
interactive=True, | |
label="FIELD") | |
with gr.Group(): | |
gr.Markdown("**MONTHLY VISITORS**") | |
with gr.Row(): | |
range_from = gr.Slider(minimum=0, | |
maximum=1_000_000, | |
value=100, | |
step=100, | |
label="FROM", | |
interactive=True) | |
range_to = gr.Slider(minimum=0, | |
maximum=1_000_000, | |
value=10_000, | |
step=100, | |
label="TO", | |
interactive=True) | |
with gr.Group(): | |
gr.Markdown("**POTENTIAL PRODUCT**") | |
with gr.Row(): | |
opt_crossSelling = gr.Checkbox(value=False, | |
label='CROSSELLING POTENTIAL', | |
interactive=True) | |
opt_similarArticles = gr.Checkbox(value=False, | |
label='SIMILARITY POTENTIAL', | |
interactive=True) | |
numberOfShops = gr.Slider(minimum=1, | |
maximum=50, | |
value=10, | |
step=1, | |
label="AMOUNT", | |
interactive=True) | |
btn = gr.Button("FIND") | |
with gr.Group(): | |
gr.Markdown("**RESULTS**") | |
out = gr.Markdown(value=' <br /> <br /> <br /> <br /> <br /> <br />', | |
label='POTENTIAL SHOPS') | |
with gr.Group(): | |
gr.Markdown("*ACCESS SECRET*") | |
loginKey = gr.Textbox(max_lines=1, | |
interactive=True, | |
type='password', | |
show_label=False) | |
btn.click(fn=findShops, | |
inputs=[platform, region, field, | |
range_from, range_to, | |
opt_crossSelling, | |
opt_similarArticles, | |
numberOfShops, | |
loginKey], | |
outputs=out) | |
demo.launch(show_api=False) | |