|
import gradio as gr |
|
import pandas as pd |
|
from groq import Groq |
|
|
|
|
|
client = Groq(api_key="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") |
|
|
|
|
|
system_message = { |
|
"role": "system", |
|
"content": "You are an experienced fashion designer. Start the conversation with a greeting, provide valuable fashion advice, and only ask questions if the user has concerns. Gather basic inputs: name, age, gender, location, height, and weight." |
|
} |
|
|
|
|
|
def reset_chat(): |
|
return [], "New Chat" |
|
|
|
|
|
def submit_questionnaire(name, age, location, gender, height, weight): |
|
|
|
questionnaire_data = { |
|
"Name": name, |
|
"Age": age, |
|
"Location": location, |
|
"Gender": gender, |
|
"Height": height, |
|
"Weight": weight |
|
} |
|
|
|
df = pd.DataFrame([questionnaire_data]) |
|
|
|
|
|
df.to_csv("questionnaire_responses.csv", mode='a', header=not pd.io.common.file_exists("questionnaire_responses.csv"), index=False) |
|
|
|
return "Thank you for completing the questionnaire!" |
|
|
|
|
|
def chat(user_input, messages, name, age, location, gender, height, weight): |
|
if user_input: |
|
|
|
user_profile_string = ( |
|
f"User profile: Name: {name}, Age: {age}, Location: {location}, " |
|
f"Gender: {gender}, Height: {height}, Weight: {weight}" |
|
) |
|
|
|
|
|
messages.append({"role": "user", "content": user_input}) |
|
messages.append(system_message) |
|
messages.append({"role": "user", "content": user_profile_string}) |
|
|
|
try: |
|
|
|
completion = client.chat.completions.create( |
|
model="llama3-8b-8192", |
|
messages=messages, |
|
temperature=1, |
|
max_tokens=1024, |
|
top_p=1, |
|
stream=False, |
|
) |
|
|
|
|
|
if completion.choices and len(completion.choices) > 0: |
|
response_content = completion.choices[0].message.content |
|
else: |
|
response_content = "Sorry, I couldn't generate a response." |
|
|
|
except Exception as e: |
|
response_content = f"Error: {str(e)}" |
|
|
|
|
|
messages.append({"role": "assistant", "content": response_content}) |
|
|
|
return messages, response_content |
|
return messages, "" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## Fashion Assistant Chatbot") |
|
|
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
name = gr.Textbox(label="Name") |
|
age = gr.Number(label="Age", value=25, minimum=1, maximum=100) |
|
location = gr.Textbox(label="Location") |
|
gender = gr.Radio(label="Gender", choices=["Male", "Female", "Other"]) |
|
height = gr.Number(label="Height (cm)", value=170, minimum=50, maximum=250) |
|
weight = gr.Number(label="Weight (kg)", value=70, minimum=20, maximum=200) |
|
|
|
with gr.Column(): |
|
submit_btn = gr.Button("Submit Questionnaire") |
|
reset_btn = gr.Button("Reset Chat") |
|
|
|
|
|
chatbox = gr.Chatbot() |
|
user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...") |
|
|
|
|
|
output_message = gr.Textbox(label="Output Message") |
|
submit_btn.click(submit_questionnaire, inputs=[name, age, location, gender, height, weight], outputs=output_message) |
|
|
|
reset_btn.click(reset_chat, outputs=[chatbox, "title"]) |
|
user_input.submit(chat, inputs=[user_input, chatbox, name, age, location, gender, height, weight], outputs=[chatbox, user_input]) |
|
|
|
|
|
demo.launch() |
|
|