Prathmesh Patil
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,100 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import random
|
3 |
+
from openai import OpenAI
|
4 |
+
from typing import List, Tuple
|
5 |
+
import time
|
6 |
|
7 |
+
class EmmaChat:
|
8 |
+
def __init__(self):
|
9 |
+
self.client = OpenAI(
|
10 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
11 |
+
api_key="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"
|
12 |
+
)
|
13 |
|
14 |
+
self.system_message = """You are Aadhya, a 17-year-old who grew up in Mumbai. You have a passion for art and enjoy playing volleyball. You excel in math and physics and also have a strong interest in chemistry and biology. You constantly seek knowledge and are willing to work hard. Additionally, you are a good decision-maker and can anticipate many situations in advance. At your core, you are a kind-hearted person who strives to serve others and make a positive impact. You work toward a better future for both yourself and humanity."""
|
15 |
|
16 |
+
def generate_response(self, message: str, history: List[Tuple[str, str]]) -> str:
|
17 |
+
# Convert history to OpenAI message format
|
18 |
+
messages = [{"role": "system", "content": self.system_message}]
|
19 |
+
for user_msg, assistant_msg in history:
|
20 |
+
messages.append({"role": "user", "content": user_msg})
|
21 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
22 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
23 |
|
24 |
+
try:
|
25 |
+
completion = self.client.chat.completions.create(
|
26 |
+
model="meta/llama-3.2-3b-instruct",
|
27 |
+
messages=messages,
|
28 |
+
temperature=0.2,
|
29 |
+
max_tokens=1024,
|
30 |
+
stream=True
|
31 |
+
)
|
32 |
|
33 |
+
# Stream the response
|
34 |
+
full_response = ""
|
35 |
+
for chunk in completion:
|
36 |
+
if chunk.choices[0].delta.content is not None:
|
37 |
+
full_response += chunk.choices[0].delta.content
|
38 |
+
yield full_response
|
39 |
|
40 |
+
except Exception as e:
|
41 |
+
yield f"I apologize, but I encountered an error: {str(e)}"
|
42 |
|
43 |
+
def create_chat_interface() -> gr.ChatInterface:
|
44 |
+
emma = EmmaChat()
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Custom CSS for the chat interface
|
47 |
+
custom_css = """
|
48 |
+
.message.user div.content {
|
49 |
+
background-color: #DCF8C6 !important;
|
50 |
+
}
|
51 |
+
.message.bot div.content {
|
52 |
+
background-color: #E8E8E8 !important;
|
53 |
+
}
|
54 |
+
.message.user, .message.bot {
|
55 |
+
padding: 1rem;
|
56 |
+
}
|
57 |
+
.avatar {
|
58 |
+
border-radius: 50%;
|
59 |
+
width: 40px;
|
60 |
+
height: 40px;
|
61 |
+
}
|
62 |
+
.message-wrap {
|
63 |
+
max-width: 800px;
|
64 |
+
margin: 0 auto;
|
65 |
+
}
|
66 |
+
"""
|
67 |
|
68 |
|
69 |
+
# Create the chat interface
|
70 |
+
chat_interface = gr.ChatInterface(
|
71 |
+
fn=emma.generate_response,
|
72 |
+
title="Chat with Aadhya 👩🏻",
|
73 |
+
description="""Aadhya is a 17-year-old from Mumbai with a passion for Art and a competitive spirit in volleyball. She excels in math, physics, chemistry, and biology, blending her analytical skills with a love for problem-solving. Driven by a desire to positively impact humanity, she is also committed to personal growth and excellence.""",
|
74 |
+
examples=[
|
75 |
+
["Hi, can you intro yourself?"],
|
76 |
+
["Is there any way I can get help from you? I'm glad to meet you."],
|
77 |
+
["I'm so glad to connect with you! Do you think we can work together on anything?"],
|
78 |
+
["How can I start a small garden at home?"]
|
79 |
+
],
|
80 |
+
theme=gr.themes.Soft(
|
81 |
+
primary_hue="pink",
|
82 |
+
secondary_hue="purple",
|
|
|
83 |
),
|
84 |
+
css=custom_css
|
85 |
+
# Removed: retry_btn="🔄 Retry",
|
86 |
+
# Removed: undo_btn="↩️ Undo",
|
87 |
+
# Removed: clear_btn="🗑️ Clear",
|
88 |
+
)
|
89 |
|
90 |
+
return chat_interface
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
+
chat_interface = create_chat_interface()
|
94 |
+
chat_interface.queue()
|
95 |
+
chat_interface.launch(
|
96 |
+
share=True,
|
97 |
+
server_name="0.0.0.0",
|
98 |
+
server_port=7000,
|
99 |
+
show_api=False
|
100 |
+
)
|