whyumesh commited on
Commit
06f9032
1 Parent(s): be31c16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -15
app.py CHANGED
@@ -8,12 +8,10 @@ import spaces
8
  device = 0 if torch.cuda.is_available() else -1
9
 
10
  def load_model():
11
- # Load the base model and tokenizer
12
  base_model_name = "Qwen/Qwen2.5-1.5B-Instruct"
13
  tokenizer = AutoTokenizer.from_pretrained(base_model_name)
14
  base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
15
 
16
- # Load the PEFT adapter
17
  peft_model = PeftModel.from_pretrained(
18
  base_model,
19
  "ombhojane/smile-small",
@@ -29,14 +27,22 @@ def load_model():
29
  pipe = load_model()
30
 
31
  @spaces.GPU
32
- def generate_response(message):
 
 
 
33
  messages = [
34
  {"role": "user", "content": message}
35
  ]
36
- # Generate longer output text
37
- generated_text = pipe(messages, max_new_tokens=200, num_return_sequences=1)
38
 
39
- # Extract only the assistant's response content
 
 
 
 
 
 
 
40
  response = generated_text[0]['generated_text']
41
  if isinstance(response, list):
42
  for msg in response:
@@ -44,15 +50,92 @@ def generate_response(message):
44
  return msg.get('content', '')
45
  return response
46
 
47
- # Create Gradio interface
48
- demo = gr.Interface(
49
- fn=generate_response,
50
- inputs=gr.Textbox(lines=2, placeholder="Enter your message here..."),
51
- outputs=gr.Textbox(lines=5),
52
- title="Smile for India",
53
- description="Smile for India is an AI assistant trained to handle Indian customer queries with cultural context awareness, benificial for Indian CRMs."
54
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # Launch the app
57
  if __name__ == "__main__":
58
  demo.launch()
 
8
  device = 0 if torch.cuda.is_available() else -1
9
 
10
  def load_model():
 
11
  base_model_name = "Qwen/Qwen2.5-1.5B-Instruct"
12
  tokenizer = AutoTokenizer.from_pretrained(base_model_name)
13
  base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
14
 
 
15
  peft_model = PeftModel.from_pretrained(
16
  base_model,
17
  "ombhojane/smile-small",
 
27
  pipe = load_model()
28
 
29
  @spaces.GPU
30
+ def generate_response(message, max_length, temperature):
31
+ if not message:
32
+ return "Please enter a message to generate a response."
33
+
34
  messages = [
35
  {"role": "user", "content": message}
36
  ]
 
 
37
 
38
+ generated_text = pipe(
39
+ messages,
40
+ max_new_tokens=max_length,
41
+ num_return_sequences=1,
42
+ temperature=temperature,
43
+ do_sample=True
44
+ )
45
+
46
  response = generated_text[0]['generated_text']
47
  if isinstance(response, list):
48
  for msg in response:
 
50
  return msg.get('content', '')
51
  return response
52
 
53
+ # Custom CSS for better styling
54
+ custom_css = """
55
+ .gradio-container {
56
+ font-family: 'Arial', sans-serif;
57
+ }
58
+ .main-title {
59
+ text-align: center;
60
+ color: #2C3E50;
61
+ margin-bottom: 1em;
62
+ }
63
+ .description {
64
+ text-align: justify;
65
+ margin-bottom: 2em;
66
+ color: #34495E;
67
+ }
68
+ """
69
+
70
+ with gr.Blocks(css=custom_css) as demo:
71
+ gr.Markdown(
72
+ """
73
+ # 🌟 S.M.I.L.E for India
74
+ ### Smart Marketing Intelligence & Local Engagement
75
+ """
76
+ )
77
+
78
+ with gr.Row():
79
+ with gr.Column(scale=2):
80
+ gr.Markdown(
81
+ """
82
+ ### About
83
+ S.M.I.L.E is an AI assistant trained to handle Indian customer queries with cultural context awareness,
84
+ beneficial for Indian CRMs. It understands local nuances, cultural preferences, and consumer behaviors
85
+ specific to the Indian market.
86
+ """
87
+ )
88
+
89
+ with gr.Row():
90
+ with gr.Column(scale=2):
91
+ input_text = gr.Textbox(
92
+ lines=4,
93
+ placeholder="Enter your query about Indian consumer behavior, cultural aspects, or market trends...",
94
+ label="Your Query"
95
+ )
96
+
97
+ with gr.Row():
98
+ max_length = gr.Slider(
99
+ minimum=50,
100
+ maximum=500,
101
+ value=200,
102
+ step=10,
103
+ label="Maximum Response Length",
104
+ info="Adjust the length of the generated response"
105
+ )
106
+ temperature = gr.Slider(
107
+ minimum=0.1,
108
+ maximum=1.0,
109
+ value=0.7,
110
+ step=0.1,
111
+ label="Temperature",
112
+ info="Higher values make the output more creative, lower values make it more focused"
113
+ )
114
+
115
+ submit_btn = gr.Button("Generate Response", variant="primary")
116
+
117
+ with gr.Column(scale=2):
118
+ output_text = gr.Textbox(
119
+ lines=12,
120
+ label="AI Response",
121
+ show_copy_button=True
122
+ )
123
+
124
+ with gr.Accordion("Sample Queries", open=False):
125
+ gr.Markdown(
126
+ """
127
+ - What are the key factors influencing Indian consumer buying behavior?
128
+ - How do cultural values affect marketing strategies in India?
129
+ - What are the popular festival shopping trends in India?
130
+ - Explain the role of family decision-making in Indian purchases
131
+ """
132
+ )
133
+
134
+ submit_btn.click(
135
+ generate_response,
136
+ inputs=[input_text, max_length, temperature],
137
+ outputs=output_text
138
+ )
139
 
 
140
  if __name__ == "__main__":
141
  demo.launch()