Prathmesh Patil commited on
Commit
d042af0
·
verified ·
1 Parent(s): 84a2b27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -49
app.py CHANGED
@@ -1,64 +1,100 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
8
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
27
 
28
- response = ""
 
29
 
30
- for message in client.chat_completion(
31
- messages,
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
- response += token
40
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
  ),
59
- ],
60
- )
 
 
 
61
 
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
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
+ )