Spaces:
Sleeping
Sleeping
import os | |
import sys | |
import subprocess | |
import streamlit as st | |
# Check if 'groq' is installed, and if not, prompt the user to install it | |
def check_groq_installed(): | |
try: | |
import groq | |
return True | |
except ImportError: | |
return False | |
# Check Python version compatibility | |
def check_python_version(): | |
required_version = (3, 6) # Minimum required Python version | |
current_version = sys.version_info | |
if current_version < required_version: | |
return False | |
return True | |
# Check if the Groq client is correctly initialized | |
def check_groq_client(): | |
try: | |
from groq import Groq | |
# Try initializing the Groq client with the provided API key | |
client = Groq(api_key="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") | |
return True | |
except Exception as e: | |
print(f"Error initializing Groq client: {e}") | |
return False | |
# Run all checks before proceeding | |
def run_checks(): | |
# Check if groq is installed | |
if not check_groq_installed(): | |
st.error("Groq module is not installed. Please install it using 'pip install groq'.") | |
return False | |
# Check if Python version is compatible | |
if not check_python_version(): | |
st.error("Python version 3.6 or higher is required. Please upgrade your Python.") | |
return False | |
# Check if the Groq client can be initialized | |
if not check_groq_client(): | |
st.error("Failed to initialize Groq client. Check your API key and the Groq module installation.") | |
return False | |
return True | |
# Function to request pattern generation from Groq API | |
def get_pattern_from_groq(body_measurements, garment_type): | |
from groq import Groq | |
client = Groq(api_key="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") | |
# Preparing the prompt for the LLM | |
prompt = f"Generate a 2D {garment_type} pattern based on the following body measurements:\n{body_measurements}" | |
# Making a request to Groq's API | |
chat_completion = client.chat.completions.create( | |
messages=[{"role": "user", "content": prompt}], | |
model="llama3-8b-8192", | |
stream=False | |
) | |
# Get the response content | |
return chat_completion.choices[0].message.content | |
# Function to simulate creating a 2D garment pattern (for demo purposes) | |
def generate_garment_pattern(body_measurements, garment_type): | |
# Here you would typically use the measurements to generate the 2D pattern. | |
# We'll simulate this by returning a simple placeholder pattern. | |
pattern = f"2D {garment_type} Pattern based on measurements:\n{body_measurements}" | |
return pattern | |
# Main Streamlit app UI | |
def main(): | |
# Perform pre-execution checks | |
if not run_checks(): | |
return | |
st.title("Garment Pattern Design Tool") | |
# Select garment type | |
garment_type = st.selectbox("Select Garment Type", ["Top Body Garment", "Bottom Garment"]) | |
# Collect body measurements based on garment type | |
st.header(f"Enter Measurements for {garment_type}") | |
body_measurements = {} | |
if garment_type == "Top Body Garment": | |
body_measurements['Chest'] = st.number_input('Chest (in cm)', min_value=0, step=1) | |
body_measurements['Waist'] = st.number_input('Waist (in cm)', min_value=0, step=1) | |
body_measurements['Neck'] = st.number_input('Neck (in cm)', min_value=0, step=1) | |
body_measurements['Height'] = st.number_input('Height (in cm)', min_value=0, step=1) | |
elif garment_type == "Bottom Garment": | |
body_measurements['Waist'] = st.number_input('Waist (in cm)', min_value=0, step=1) | |
body_measurements['Hip'] = st.number_input('Hip (in cm)', min_value=0, step=1) | |
body_measurements['Inseam'] = st.number_input('Inseam (in cm)', min_value=0, step=1) | |
body_measurements['Height'] = st.number_input('Height (in cm)', min_value=0, step=1) | |
body_measurements_str = "\n".join([f"{key}: {value}" for key, value in body_measurements.items()]) | |
if st.button("Generate Pattern"): | |
if all(value > 0 for value in body_measurements.values()): | |
# Fetch pattern from Groq LLM | |
st.write("Generating pattern...") | |
pattern = get_pattern_from_groq(body_measurements_str, garment_type) | |
# Simulate the 2D pattern generation (you can replace this with actual pattern generation logic) | |
pattern_output = generate_garment_pattern(body_measurements_str, garment_type) | |
# Display the result | |