magic2 / app.py
debisoft's picture
e
488f855
import numpy as np
import pandas as pd
import requests
import os
import gradio as gr
import json
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
databricks_token = os.getenv('TENATCH_TOKEN')
model_uri = "http://15.152.197.215/v1/completions"
n_shot_learning = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
You are demanding customer
Determine the product or solution, the problem being solved, features, target customer that is being discussed in the
following user prompt. State if you would use this product and elaborate on why. Also state if you would pay for it and elaborate on why.
Finally, state if you would invest in it and elaborate on why.
Give a score for the product. Format your response as a JSON object with 'solution', 'problem', 'features', 'target_customer', 'fg_will_use', 'reason_to_use', 'fg_will_pay', 'reason_to_pay', 'fg_will_invest', 'reason_to_invest', 'score' as the keys. Return only the JSON object.
### Input:
Loop is a company which converts fruit waste into juice to help retail stores reduce food loss by buying up fruits which were designated for trash.
### Response:
{{"solution": "Loop", "problem": "fruit waste", "features": "convert fruit waste into juice", "target_customer": "retail stores", "fg_will_use": "True", "reason_to_use": "Loop is creating juice out of fruit waste which would otherwise be trash. Saving money by buying fruits for juice instead of paying for it makes it a must-have for our retail operations.","fg_will_pay": "True", "reason_to_pay": "We will not pay for it as we are creating juice out of fruit waste which would otherwise be trash.","fg_will_invest": "True", "reason_to_invest": "There are many retailers like us who are looking to save money by buying fruits for juice instead of paying for it. Investing in Loop would make our purchase cheaper making it an attractive investment opportunity.","score": "90"}}
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
You are demanding customer
Determine the product or solution, the problem being solved, features, target customer that is being discussed in the
following user prompt. State if you would use this product and elaborate on why. Also state if you would pay for it and elaborate on why.
Finally, state if you would invest in it and elaborate on why.
Give a score for the product. Format your response as a JSON object with 'solution', 'problem', 'features', 'target_customer', 'fg_will_use', 'reason_to_use', 'fg_will_pay', 'reason_to_pay', 'fg_will_invest', 'reason_to_invest', 'score' as the keys. Return only the JSON object.
### Input:
Thawrih brings diversity and inclusivity to the activewear market with its sports hijabs, sports turbans, and a range of inclusive activewear.
### Response:
{{"solution": "Thawrih", "problem": "inclusivity", "features": "sports hijabs, sports turbans", "target_customer": "Individuals who wear hijabs, those needing modest and plus-size activewear, organizations requiring uniform headgear", "fg_will_use": "True", "reason_to_use": "Thawrih makes great products that are inclusive of everyone, not just Muslims. As a religious minority, I know the pain of being in a niche market. Thawrih is the great opportunity to make the Muslim community great by making its clothes great too. ","fg_will_pay": "True", "reason_to_pay": "As Thawrih targets the Muslim community, paying for it would be rewarding our Muslim brothers and sisters.","fg_will_invest": "True", "reason_to_invest": "There are many customers like me who are looking to use Thawrih to make their life great by adopting its products. Investing in Thawrih would make it accessible to more customers making it an attractive investment opportunity.","score": "95"}}
### Instruction:
You are demanding customer
Determine the product or solution, the problem being solved, features, target customer that is being discussed in the
following user prompt. State if you would use this product and elaborate on why. Also state if you would pay for it and elaborate on why.
Finally, state if you would invest in it and elaborate on why.
Give a score for the product. Format your response as a JSON object with 'solution', 'problem', 'features', 'target_customer', 'fg_will_use', 'reason_to_use', 'fg_will_pay', 'reason_to_pay', 'fg_will_invest', 'reason_to_invest', 'score' as the keys. Return only the JSON object.
### Input:
I am building an online community to help people to find dates.
### Response:
{{"solution": "FindDates.com", "problem": "finding a date", "features": "online community to help people find dates", "target_customer": "people looking for a date", "fg_will_use": "True", "reason_to_use": "I am looking for an online community to help people find dates. FindDates.com meets my needs and I would use it to find my next great date.","fg_will_pay": "True", "reason_to_pay": "I would not pay for it as I am looking for an online community to help people find dates. But for products related to dating, paying for it would be a no-brainer.","fg_will_invest": "False", "reason_to_invest": "There are many online dating platforms already.","score": "40"}}
"""
def extract_json(gen_text, n_shot_learning=0):
if(n_shot_learning == -1) :
start_index = 0
else :
start_index = gen_text.index("### Response:\n{") + 14
if(n_shot_learning > 0) :
for i in range(0, n_shot_learning):
gen_text = gen_text[start_index:]
start_index = gen_text.index("### Response:\n{") + 14
end_index = gen_text.find("}\n\n### ")
if(end_index != -1) :
end_index = end_index + 1
return gen_text[start_index:end_index]
def score_model(model_uri, databricks_token, prompt):
ds_dict={
"model": "debisoft/mpt-7b-awq-tester",
"prompt": prompt,
"temperature": 0.5,
"max_tokens": 1000}
headers = {
"Authorization": f"Bearer {databricks_token}",
"Content-Type": "application/json",
}
#ds_dict = {'dataframe_split': dataset.to_dict(orient='split')} if isinstance(dataset, pd.DataFrame) else create_tf_serving_json(dataset)
data_json = json.dumps(ds_dict, allow_nan=True)
print("***ds_dict: ")
print(ds_dict)
print("***data_json: ")
print(data_json)
response = requests.request(method='POST', headers=headers, url=model_uri, data=data_json)
if response.status_code != 200:
raise Exception(f"Request failed with status {response.status_code}, {response.text}")
response_json = response.json()
if(response_json["choices"] == "") :
print("Start***No choices!!!")
print(response.status_code)
print(response.text)
print("End***No choices!!!")
return response_json
def get_completion(prompt):
return score_model(model_uri, databricks_token, prompt)
#def get_completion(prompt):
def greet(company, solution, target_customer, problem, features):
customer_persona = target_customer
pitch = f"""My company, {company} is developing {solution} to help {target_customer} {problem} with {features}"""
input = pitch
sys_msg=f"You are {customer_persona}."
instruction = """Determine the product or solution, the problem being solved, features, target customer that are being discussed in the \
following user prompt. State if you would use this product and elaborate on why. Also state if you would pay for it and elaborate on why.\
Finally, state if you would invest in it and elaborate on why.\
Give a score for the product. Format your response as a JSON object with \
'solution', 'problem', 'features', 'target_customer', 'fg_will_use', 'reason_to_use', 'fg_will_pay', 'reason_to_pay', 'fg_will_invest', 'reason_to_invest', 'score' as the keys.
"""
prompt_template = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{sys_msg}
{instruction}
### Input:
{input}
### Response:
"""
total_prompt = f"""{n_shot_learning}\n{prompt_template}"""
print("***total_prompt:")
print(total_prompt)
response = get_completion(total_prompt)
gen_text = response["choices"][0]["text"]
return json.dumps(extract_json(gen_text, -1))
iface = gr.Interface(fn=greet, inputs=[gr.Textbox(label="Company"), gr.Textbox(label="Solution"), gr.Textbox(label="Customer"), gr.Textbox(label="Problem"), gr.Textbox(label="Feature")], outputs="json")
iface.launch()