Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# First cell: Install required dependencies
|
2 |
+
#!pip install transformers accelerate gradio --quiet
|
3 |
+
|
4 |
+
# Second cell: Import required libraries and check GPU
|
5 |
+
import torch
|
6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Check for GPU availability
|
10 |
+
if torch.cuda.is_available():
|
11 |
+
device = torch.device("cuda")
|
12 |
+
print("Using GPU:", torch.cuda.get_device_name(0))
|
13 |
+
print("Memory Available:", torch.cuda.get_device_properties(0).total_memory / 1e9, "GB")
|
14 |
+
else:
|
15 |
+
device = torch.device("cpu")
|
16 |
+
print("Using CPU")
|
17 |
+
|
18 |
+
# Third cell: Load model and tokenizer
|
19 |
+
def initialize_model():
|
20 |
+
model_name = "microsoft/Phi-3.5-mini-instruct"
|
21 |
+
print(f"Loading {model_name}...")
|
22 |
+
|
23 |
+
# Load with lower precision for GPU efficiency
|
24 |
+
if device.type == "cuda":
|
25 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
26 |
+
model_name,
|
27 |
+
trust_remote_code=True
|
28 |
+
)
|
29 |
+
model = AutoModelForCausalLM.from_pretrained(
|
30 |
+
model_name,
|
31 |
+
torch_dtype=torch.float16,
|
32 |
+
device_map="auto",
|
33 |
+
trust_remote_code=True
|
34 |
+
)
|
35 |
+
else:
|
36 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
37 |
+
model_name,
|
38 |
+
trust_remote_code=True
|
39 |
+
)
|
40 |
+
model = AutoModelForCausalLM.from_pretrained(
|
41 |
+
model_name,
|
42 |
+
trust_remote_code=True
|
43 |
+
).to(device)
|
44 |
+
|
45 |
+
return model, tokenizer
|
46 |
+
|
47 |
+
try:
|
48 |
+
model, tokenizer = initialize_model()
|
49 |
+
|
50 |
+
# Create pipeline
|
51 |
+
problem_solver_pipeline = pipeline(
|
52 |
+
"text-generation",
|
53 |
+
model=model,
|
54 |
+
tokenizer=tokenizer,
|
55 |
+
device=0 if device.type == "cuda" else -1,
|
56 |
+
max_length=500
|
57 |
+
)
|
58 |
+
print("Model loaded successfully!")
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error loading model: {str(e)}")
|
61 |
+
raise
|
62 |
+
|
63 |
+
# Fourth cell: Define analysis function with improved prompting for Phi-3.5
|
64 |
+
def analyze_idea(idea, max_length=500, temperature=0.7):
|
65 |
+
"""
|
66 |
+
Analyze an input idea using the Phi-3.5 model.
|
67 |
+
"""
|
68 |
+
if not idea.strip():
|
69 |
+
return "Please enter an idea to analyze."
|
70 |
+
|
71 |
+
prompt = f"""Instruction: Analyze the following business idea and provide a structured analysis identifying core problems and their solutions.
|
72 |
+
|
73 |
+
Input idea: "{idea}"
|
74 |
+
|
75 |
+
Please structure your response in the following format:
|
76 |
+
1. List the main problems that could arise
|
77 |
+
2. Provide specific solutions for each problem
|
78 |
+
3. Give a brief summary of the overall analysis
|
79 |
+
|
80 |
+
Response:"""
|
81 |
+
|
82 |
+
try:
|
83 |
+
# Generate response with error handling
|
84 |
+
response = problem_solver_pipeline(
|
85 |
+
prompt,
|
86 |
+
max_length=max_length,
|
87 |
+
temperature=temperature,
|
88 |
+
num_return_sequences=1,
|
89 |
+
pad_token_id=tokenizer.eos_token_id,
|
90 |
+
do_sample=True,
|
91 |
+
top_p=0.9
|
92 |
+
)
|
93 |
+
|
94 |
+
output = response[0]["generated_text"]
|
95 |
+
|
96 |
+
# Format the final output
|
97 |
+
formatted_output = f"""#### Input Idea:
|
98 |
+
"{idea}"
|
99 |
+
|
100 |
+
#### Analysis:
|
101 |
+
{output.replace(prompt, '')}""" # Remove the prompt from the output
|
102 |
+
|
103 |
+
return formatted_output
|
104 |
+
|
105 |
+
except Exception as e:
|
106 |
+
return f"An error occurred: {str(e)}"
|
107 |
+
|
108 |
+
# Fifth cell: Create and launch Gradio interface
|
109 |
+
def create_gradio_interface():
|
110 |
+
interface = gr.Interface(
|
111 |
+
fn=analyze_idea,
|
112 |
+
inputs=[
|
113 |
+
gr.Textbox(
|
114 |
+
lines=5,
|
115 |
+
placeholder="Enter your business idea here. For example: 'A mobile app that connects local food trucks with nearby customers in real-time.'",
|
116 |
+
label="Your Business Idea"
|
117 |
+
),
|
118 |
+
gr.Slider(
|
119 |
+
minimum=100,
|
120 |
+
maximum=1000,
|
121 |
+
value=500,
|
122 |
+
step=50,
|
123 |
+
label="Response Length"
|
124 |
+
),
|
125 |
+
gr.Slider(
|
126 |
+
minimum=0.1,
|
127 |
+
maximum=1.0,
|
128 |
+
value=0.7,
|
129 |
+
step=0.1,
|
130 |
+
label="Creativity (Temperature)"
|
131 |
+
)
|
132 |
+
],
|
133 |
+
outputs=gr.Textbox(
|
134 |
+
label="Analysis Results",
|
135 |
+
lines=12
|
136 |
+
),
|
137 |
+
title="Business Idea Analyzer powered by Phi-3.5",
|
138 |
+
description="Enter your business idea, and this AI-powered tool will analyze potential problems, suggest solutions, and provide a summary.",
|
139 |
+
examples=[
|
140 |
+
["An AI-powered platform for personalized workout recommendations based on real-time fitness tracking data.", 500, 0.7],
|
141 |
+
["A subscription service for sustainable, package-free household products with local delivery.", 500, 0.7],
|
142 |
+
["A marketplace connecting local artists with businesses looking for unique office artwork.", 500, 0.7]
|
143 |
+
]
|
144 |
+
)
|
145 |
+
return interface
|
146 |
+
|
147 |
+
# Launch the interface
|
148 |
+
interface = create_gradio_interface()
|
149 |
+
interface.launch(share=True, debug=True)
|