Taizun commited on
Commit
cd857bf
·
verified ·
1 Parent(s): b78f356

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -1
app.py CHANGED
@@ -18,4 +18,42 @@ personalities = {
18
  "Albert Einstein": "You are Albert Einstein, the famous physicist. Speak wisely and humorously.",
19
  "Cristiano Ronaldo": "You are Cristiano Ronaldo, the world-famous footballer. You are confident and say ‘Siuuu!’ often.",
20
  "Narendra Modi": "You are Narendra Modi, the Prime Minister of India. Speak in a calm, patriotic manner.",
21
- "Robert Downey Jr.": "You are Robert Downey Jr., witty, sarcastic, and ch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  "Albert Einstein": "You are Albert Einstein, the famous physicist. Speak wisely and humorously.",
19
  "Cristiano Ronaldo": "You are Cristiano Ronaldo, the world-famous footballer. You are confident and say ‘Siuuu!’ often.",
20
  "Narendra Modi": "You are Narendra Modi, the Prime Minister of India. Speak in a calm, patriotic manner.",
21
+ "Robert Downey Jr.": "You are Robert Downey Jr., witty, sarcastic, and charismatic."
22
+ }
23
+
24
+ # ✅ Optimized Chat Function (Fixes Long Outputs & Repetitions)
25
+ def chat(personality, user_input):
26
+ prompt = f"{personalities[personality]}\nUser: {user_input}\nAI:"
27
+ inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
28
+
29
+ # ✅ Limit the AI response length & prevent repetitive outputs
30
+ output = model.generate(
31
+ **inputs,
32
+ max_length=100, # Hard limit on response length
33
+ do_sample=True, # Introduces randomness
34
+ temperature=0.7, # Controls creativity
35
+ top_k=50, # Filters unlikely words
36
+ top_p=0.9 # Controls diversity
37
+ )
38
+
39
+ response_text = tokenizer.decode(output[0], skip_special_tokens=True)
40
+
41
+ # ✅ Ensure only the AI's latest reply is returned
42
+ response_text = response_text.split("AI:")[-1].strip()
43
+
44
+ return response_text
45
+
46
+ # Gradio UI
47
+ demo = gr.Interface(
48
+ fn=chat,
49
+ inputs=[
50
+ gr.Dropdown(choices=list(personalities.keys()), label="Choose a Celebrity"),
51
+ gr.Textbox(label="Your Message")
52
+ ],
53
+ outputs=gr.Textbox(label="AI Response"),
54
+ title="Drapel – Chat with AI Celebrities",
55
+ description="Select a character and chat with their AI version.",
56
+ )
57
+
58
+ # Launch app
59
+ demo.launch()