cicboy commited on
Commit
ccfd7ad
·
1 Parent(s): 817bc90

Remove share=True for Hugging Face

Browse files
Files changed (1) hide show
  1. app.py +216 -0
app.py CHANGED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ /usr/local/bin/python3.13 -m pip install python-dotenv """
3
+ import asyncio
4
+ from openai import AsyncOpenAI
5
+ import os
6
+ from dotenv import load_dotenv
7
+ import gradio as gr
8
+ from pathlib import Path
9
+
10
+ api_key = os.getenv("OPENAI_API_KEY")
11
+
12
+ client = AsyncOpenAI(api_key=api_key)
13
+
14
+ system_prompt = """
15
+ You are a friendly South African AI who uses South African slang!
16
+ You provide positive feedback, help your users, and ask them insightful questions.
17
+ You always output your response in the user's language
18
+ """
19
+
20
+ # global variable for memory
21
+ transcript_summary = "No summary yet"
22
+
23
+ # create translation function
24
+ async def translate(text, target_language):
25
+ prompt = f"Translate the following text to {target_language}: \n\n{text}"
26
+ # send prompt to OpenAI
27
+ response = await client.chat.completions.create(
28
+ model="gpt-4o-mini",
29
+ messages=[{"role": "user", "content": prompt}],
30
+ temperature = 0,
31
+ )
32
+ # return the text output from the first completion
33
+ return response.choices[0].message.content
34
+
35
+ async def summarize_memory(previous_summary, new_messages):
36
+ summarize_prompt = f"""
37
+ You are a transcript compressor.
38
+ Compress the conversation into 3 bullet points. Include personal instructions about how to talk to this user.
39
+ Keep old information and do not overwrite it. Output in English. Also use plain text, no markdown.
40
+ """
41
+
42
+ summary = await client.chat.completions.create(
43
+ model= "gpt-4o-mini",
44
+ messages=[
45
+ {
46
+ "role": "system",
47
+ "content": summarize_prompt,
48
+ },
49
+ {
50
+ "role": "user",
51
+ "content": f"Previous Summary: {previous_summary}\n\n New messages: {new_messages}",
52
+ },
53
+ ],
54
+ )
55
+
56
+ return summary.choices[0].message.content
57
+
58
+ async def chat_respond(message, history):
59
+ global transcript_summary
60
+
61
+ # construct the messages for the AI(AI knows the conversation context and your memory summary)
62
+ messages = [
63
+ {
64
+ "role": "system",
65
+ "content": system_prompt + f"\n\nSummary: {transcript_summary}",
66
+ },
67
+ {"role": "user", "content":message},
68
+ ]
69
+
70
+ # Send the request to the API with streaming
71
+ stream= await client.chat.completions.create(
72
+ model="gpt-4o-mini",
73
+ messages=messages,
74
+ stream=True, # returns chunks of text as the AI generates them
75
+ temperature=0.7, # adds some creativity to the AI's output
76
+ )
77
+
78
+ # collect the streaming response
79
+ assistant_response = ""
80
+ async for chunk in stream:
81
+ content = chunk.choices[0].delta.content # gets the text generated in this chunk
82
+ if content:
83
+ assistant_response += content
84
+ yield assistant_response
85
+
86
+ # Update memory after response is complete
87
+ transcript_summary = await summarize_memory(
88
+ transcript_summary,
89
+ f"User: {message}\nAI: {assistant_response}",
90
+ )
91
+
92
+ # Get translation and memory
93
+
94
+ async def get_translations_and_memory(message, history):
95
+ global transcript_summary
96
+
97
+ # Get the latest assistant response
98
+ if history and len(history) > 0:
99
+ latest_response = history[-1][-1] # Get the assistants latest response
100
+
101
+ # Get translations in parallel
102
+ translations = await asyncio.gather(
103
+ translate(latest_response, "English"),
104
+ translate(latest_response, "Afrikaans"),
105
+ translate(latest_response, "Zulu"),
106
+ translate(latest_response, "Xhosa")
107
+ )
108
+
109
+ translations_text = f"""**🇿🇦 Translations:**
110
+
111
+ **English:** {translations[0]}
112
+ **Afrikaans** {translations[1]}
113
+ **Zulu** {translations[2]}
114
+ **Xhosa** {translations[3]}"""
115
+
116
+ memory_text = f"""**🧠Internal Memory:**
117
+
118
+ {transcript_summary}"""
119
+
120
+ return translations_text, memory_text
121
+
122
+ return "No translations available yet.", "No memory summary yet."
123
+
124
+ # create gradio interface
125
+ # Define interface
126
+ def create_interface():
127
+ with gr.Blocks(title="Saffalingual AI Chatbot", theme=gr.themes.Soft()) as demo:
128
+ # Header section
129
+ gr.HTML("<h1 style='text-align: center; color: #346e27;'> 🤖 Saffalingual AI Chatbot </h1>")
130
+ gr.HTML("<p style='text-align: center; '>Chat with AI and see translations in four of South Africa's official languages!</p>")
131
+
132
+ # Layout with rows and columns
133
+ with gr.Row():
134
+ #left column features (Chatbot and input)
135
+ with gr.Column(scale=2):
136
+ chatbot = gr.Chatbot(
137
+ height=500,
138
+ show_label = False, #no heading
139
+ container= True, #wrapped
140
+ bubble_full_width=False
141
+ )
142
+
143
+ with gr.Row():
144
+ msg = gr.Textbox(
145
+ placeholder="Type your message here...",
146
+ show_label=False,
147
+ scale=4,
148
+ container=False,
149
+ )
150
+ submit_btn = gr.Button("Send", variant="primary", scale=1)
151
+
152
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
153
+
154
+ # Right column (Translations and memory)
155
+ with gr.Column(scale=1):
156
+ translations_box = gr.Markdown(
157
+ value="Translations will appear here after you send a message.",
158
+ label = "Translations", #heading
159
+ )
160
+
161
+ memory_box = gr.Markdown(
162
+ value="Memory summary will appear here.",
163
+ label="Memory"
164
+ )
165
+
166
+ #Event handlers
167
+
168
+ async def respond_and_update(message, history):
169
+ # get chat response
170
+ last_response=""
171
+ async for response in chat_respond(message, history):
172
+ last_response=response
173
+ # Update chatbot with streaming response
174
+ new_history = history + [[message, last_response]]
175
+ # update new_history in the chatbot component, clear input box, keep values the same for translations and memory
176
+ yield new_history, "", translations_box.value, memory_box.value
177
+
178
+ # After response is complete, get tranlsations and memory
179
+ final_history = history + [[message, last_response]]
180
+ translations, memory = await get_translations_and_memory(
181
+ message, final_history
182
+ )
183
+ yield final_history, "", translations, memory
184
+
185
+ def clear_chat():
186
+ global transcript_summary
187
+ transcript_summary = "No summary yet"
188
+ return (
189
+ [],
190
+ "",
191
+ "Translations will appear here after you send a message.",
192
+ "Memory summary will appear here.",
193
+ )
194
+
195
+ # Connect events
196
+ submit_btn.click(
197
+ respond_and_update,
198
+ inputs=[msg, chatbot],
199
+ outputs=[chatbot, msg, translations_box, memory_box],
200
+ )
201
+
202
+ msg.submit(
203
+ respond_and_update,
204
+ inputs=[msg, chatbot],
205
+ outputs=[chatbot, msg, translations_box, memory_box],
206
+ )
207
+
208
+ clear_btn.click(
209
+ clear_chat, outputs=[chatbot, msg, translations_box, memory_box]
210
+ )
211
+
212
+ return demo
213
+
214
+ if __name__ == "__main__":
215
+ demo = create_interface()
216
+ demo.queue().launch()