vmagotr1 commited on
Commit
10e90f5
·
verified ·
1 Parent(s): b95201b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -102
app.py CHANGED
@@ -1,127 +1,83 @@
1
- # import gradio as gr
2
- # from huggingface_hub import InferenceClient
3
-
4
- # # Step 1: Read your background info
5
- # with open("BACKGROUND.md", "r", encoding="utf-8") as f:
6
- # background_text = f.read()
7
-
8
- # # Step 2: Set up your InferenceClient (same as before)
9
- # client = InferenceClient("google/gemma-2-2b-jpn-it")
10
- # # HuggingFaceH4/zephyr-7b-beta
11
- # def respond(
12
- # message,
13
- # history: list[dict],
14
- # system_message: str,
15
- # max_tokens: int,
16
- # temperature: float,
17
- # top_p: float,
18
- # ):
19
- # if history is None:
20
- # history = []
21
-
22
- # # Include background text as part of the system message for context
23
- # combined_system_message = f"{system_message}\n\n### Background Information ###\n{background_text}"
24
-
25
- # # Start building the conversation history
26
- # messages = [{"role": "system", "content": combined_system_message}]
27
-
28
- # # Add conversation history
29
- # for interaction in history:
30
- # if "user" in interaction:
31
- # messages.append({"role": "user", "content": interaction["user"]})
32
- # if "assistant" in interaction:
33
- # messages.append({"role": "assistant", "content": interaction["assistant"]})
34
-
35
- # # Add the latest user message
36
- # messages.append({"role": "user", "content": message})
37
-
38
- # # Generate response
39
- # response = ""
40
- # for msg in client.chat_completion(
41
- # messages,
42
- # max_tokens=max_tokens,
43
- # stream=True,
44
- # temperature=temperature,
45
- # top_p=top_p,
46
- # ):
47
-
48
- # token = msg.choices[0].delta.content
49
- # response += token
50
- # yield response
51
- # print("----- SYSTEM MESSAGE -----")
52
- # print(messages[0]["content"])
53
- # print("----- FULL MESSAGES LIST -----")
54
- # for m in messages:
55
- # print(m)
56
- # print("-------------------------")
57
-
58
- # # Step 3: Build a Gradio Blocks interface with two Tabs
59
- # with gr.Blocks() as demo:
60
- # # Tab 1: GPT Chat Agent
61
- # with gr.Tab("GPT Chat Agent"):
62
- # gr.Markdown("## Welcome to Varun's GPT Agent")
63
- # gr.Markdown("Feel free to ask questions about Varun’s journey, skills, and more!")
64
- # chat = gr.ChatInterface(
65
- # fn=respond,
66
- # additional_inputs=[
67
- # gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
68
- # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
69
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
70
- # gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
71
- # ],
72
- # type="messages", # Specify message type
73
- # )
74
-
75
- # # # Tab 2: Background Document
76
- # # with gr.Tab("Varun's Background"):
77
- # # gr.Markdown("# About Varun")
78
- # # gr.Markdown(background_text)
79
-
80
- # # Step 4: Launch
81
- # if __name__ == "__main__":
82
- # demo.launch()
83
-
84
  import gradio as gr
85
  from huggingface_hub import InferenceClient
86
 
87
- client = InferenceClient("google/gemma-2-2b-jpn-it")
88
-
89
- def respond(message, history, system_message, max_tokens, temperature, top_p):
 
 
 
 
 
 
 
 
 
 
 
 
90
  if history is None:
91
  history = []
92
 
93
- prompt = f"{system_message}\n\n# Background...\n\n" # etc.
94
- # Build up your prompt from history...
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
 
96
  response = ""
97
- for chunk in client.text_generation(
98
- prompt=prompt,
99
- max_new_tokens=max_tokens,
 
100
  temperature=temperature,
101
  top_p=top_p,
102
- stream=True,
103
  ):
104
- # 'chunk' is a string of newly generated text.
105
- response += chunk
 
106
  yield response
107
-
108
- # (Optional) log the final prompt
109
- print("PROMPT:", prompt)
110
-
111
-
 
 
 
112
  with gr.Blocks() as demo:
113
- with gr.Tab("Gemma Chat Agent"):
 
 
 
114
  chat = gr.ChatInterface(
115
  fn=respond,
116
  additional_inputs=[
117
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
118
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
119
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
120
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
121
  ],
122
- type="messages",
123
  )
124
 
 
 
 
 
 
 
125
  if __name__ == "__main__":
126
  demo.launch()
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Step 1: Read your background info
5
+ with open("BACKGROUND.md", "r", encoding="utf-8") as f:
6
+ background_text = f.read()
7
+
8
+ # Step 2: Set up your InferenceClient (same as before)
9
+ client = InferenceClient("meta-llama/Llama-3.2-1B")
10
+ # HuggingFaceH4/zephyr-7b-beta
11
+ def respond(
12
+ message,
13
+ history: list[dict],
14
+ system_message: str,
15
+ max_tokens: int,
16
+ temperature: float,
17
+ top_p: float,
18
+ ):
19
  if history is None:
20
  history = []
21
 
22
+ # Include background text as part of the system message for context
23
+ combined_system_message = f"{system_message}\n\n### Background Information ###\n{background_text}"
24
+
25
+ # Start building the conversation history
26
+ messages = [{"role": "system", "content": combined_system_message}]
27
+
28
+ # Add conversation history
29
+ for interaction in history:
30
+ if "user" in interaction:
31
+ messages.append({"role": "user", "content": interaction["user"]})
32
+ if "assistant" in interaction:
33
+ messages.append({"role": "assistant", "content": interaction["assistant"]})
34
+
35
+ # Add the latest user message
36
+ messages.append({"role": "user", "content": message})
37
 
38
+ # Generate response
39
  response = ""
40
+ for msg in client.chat_completion(
41
+ messages,
42
+ max_tokens=max_tokens,
43
+ stream=True,
44
  temperature=temperature,
45
  top_p=top_p,
 
46
  ):
47
+
48
+ token = msg.choices[0].delta.content
49
+ response += token
50
  yield response
51
+ print("----- SYSTEM MESSAGE -----")
52
+ print(messages[0]["content"])
53
+ print("----- FULL MESSAGES LIST -----")
54
+ for m in messages:
55
+ print(m)
56
+ print("-------------------------")
57
+
58
+ # Step 3: Build a Gradio Blocks interface with two Tabs
59
  with gr.Blocks() as demo:
60
+ # Tab 1: GPT Chat Agent
61
+ with gr.Tab("GPT Chat Agent"):
62
+ gr.Markdown("## Welcome to Varun's GPT Agent")
63
+ gr.Markdown("Feel free to ask questions about Varun’s journey, skills, and more!")
64
  chat = gr.ChatInterface(
65
  fn=respond,
66
  additional_inputs=[
67
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
68
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
69
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
70
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
71
  ],
72
+ type="messages", # Specify message type
73
  )
74
 
75
+ # # Tab 2: Background Document
76
+ # with gr.Tab("Varun's Background"):
77
+ # gr.Markdown("# About Varun")
78
+ # gr.Markdown(background_text)
79
+
80
+ # Step 4: Launch
81
  if __name__ == "__main__":
82
  demo.launch()
83