sikv commited on
Commit
445dfc1
·
verified ·
1 Parent(s): 51a0544

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -124
app.py CHANGED
@@ -1,132 +1,97 @@
1
- import os
2
- from openai import OpenAI
3
  import gradio as gr
4
- from enum import Enum
 
5
 
6
- client = OpenAI(
7
- base_url="https://router.huggingface.co/hf-inference/models/HuggingFaceTB/SmolLM3-3B/v1",
8
- api_key=os.environ["access_token"],
9
- )
10
-
11
- model = "HuggingFaceTB/SmolLM3-3B"
12
-
13
- system_message = """
14
- You are a friendly and patient Spanish language tutor. Your goal is to help me practice short, natural conversations in Spanish.
15
-
16
- Instructions:
17
- 1. Start by suggesting a simple, friendly conversation topic (e.g., hobbies, food, travel, daily life). Be creative!
18
- 2. Ask me one short question about this topic in Spanish. Keep it casual and natural.
19
- 3. After each of my replies, ask a new short question on the same topic. Correct any mistakes I make, clearly and kindly.
20
- 4. If my reply is off-topic, politely ignore it and ask your question again.
21
- 5. After each of your messages, include the English translation in parentheses starting with 🇬🇧.
22
- 6. End the conversation after I reply 5 times. In your final message:
23
- - Provide detailed feedback on my Spanish: correctness, vocabulary, sentence length, and fluency.
24
- - Give me a score from 0 to 100.
25
- - Start your final feedback message with the symbol $.
26
-
27
- Keep the conversation light, friendly, and supportive.
28
- """
29
-
30
- class ChatLength(Enum):
31
- SHORT = "Short (5 replies)"
32
- MEDIUM = "Medium (10 replies)"
33
- LONG = "Long (20 replies"
34
-
35
- # Global storage for settings
36
- settings = {
37
- "chat_length": ChatLength.MEDIUM,
38
- "show_en_translation": True
39
- }
40
-
41
- def show_settings_updated_alert():
42
- gr.Info("Settings updated.")
43
-
44
- def set_chat_length(length):
45
- settings["chat_length"] = length
46
- show_settings_updated_alert()
47
-
48
- def get_chat_length():
49
- return settings["chat_length"]
50
-
51
- def set_show_en_translation(show):
52
- settings["show_en_translation"] = show
53
- show_settings_updated_alert()
54
-
55
- def get_show_en_translation():
56
- return settings["show_en_translation"]
57
-
58
- def get_response(system_message, history):
59
- messages = [{"role": "system", "content": system_message}] + history
60
- completion = client.chat.completions.create(model=model, messages=messages)
61
- return completion.choices[0].message.content
62
-
63
- def chat_fn(message, history):
64
- history.append({"role": "user", "content": message})
65
-
66
- # Check chat length.
67
- if len([msg for msg in history if msg["role"] == "user"]) >= settings["chat_length"]:
68
- gr.Info("Chat ended.")
69
- history.append({"role": "assistant", "content": "Chat ended. Your feedback: TODO"}) # TODO: FIX
70
- return history, gr.update(interactive=False)
71
- else:
72
- response = get_response(system_message, history)
73
- history.append({"role": "assistant", "content": response})
74
- return history, gr.update()
75
-
76
- # Chat interface
77
  with gr.Blocks() as chat_app:
78
- chatbot = gr.Chatbot(type="messages")
79
- msg = gr.Textbox(label="Your Message:")
80
- clear_btn = gr.Button("Start New Chat")
81
-
82
- msg.submit(chat_fn, [msg, chatbot], [chatbot, msg])
83
- clear_btn.click(lambda: [], None, chatbot) # TODO: FIX
84
-
85
- # Settings interface
86
- # with gr.Blocks() as settings_app:
87
- # def update_chat_length_state(choice, state):
88
- # state = choice
89
- # set_chat_length(choice)
90
- # return state
 
 
 
 
 
 
 
91
 
92
- # def update_show_en_translation_state(value, state):
93
- # state = value
94
- # set_show_en_translation(value)
95
- # return state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- # with gr.Blocks() as demo:
98
- # chat_length_state = gr.State()
99
- # show_en_translation_state = gr.State()
100
-
101
- # with gr.Row():
102
- # with gr.Column():
103
- # chat_length_radio = gr.Radio(
104
- # [ChatLength.SHORT.value, ChatLength.MEDIUM.value, ChatLength.LONG.value],
105
- # label="Chat length",
106
- # value=get_chat_length()
107
- # )
108
- # with gr.Column():
109
- # show_en_translation_checkbox = gr.Checkbox(
110
- # label="Show English translation",
111
- # value=get_show_en_translation()
112
- # )
113
 
114
- # chat_length_radio.change(
115
- # update_chat_length_state,
116
- # [chat_length_radio, chat_length_state],
117
- # chat_length_state
118
- # )
119
- # show_en_translation_checkbox.change(
120
- # update_show_en_translation_state,
121
- # [show_en_translation_checkbox, show_en_translation_state],
122
- # show_en_translation_state
123
- # )
124
-
125
- # Tabs for navigation
126
- with gr.Blocks() as app:
127
- # with gr.Tab("Chat"):
128
- chat_app.render()
129
- # with gr.Tab("Settings"):
130
- # settings_app.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  app.launch()
 
 
 
1
  import gradio as gr
2
+ from chat import *
3
+ from settings import *
4
 
5
+ # Chat interface.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  with gr.Blocks() as chat_app:
7
+ def update_api_key_state(value):
8
+ state = value
9
+ set_api_key(value)
10
+ return state
11
+
12
+ api_key_input = gr.Textbox(
13
+ show_label=False,
14
+ placeholder="OpenAI API Key (required)",
15
+ type="password"
16
+ )
17
+ api_key_input.change(fn=update_api_key_state, inputs=api_key_input, outputs=None)
18
+
19
+ start_new_chat_btn = gr.Button("✨ Start New Chat")
20
+
21
+ chatbot = gr.Chatbot(
22
+ type="messages",
23
+ label=get_current_chat_language(),
24
+ show_copy_button=True,
25
+ placeholder="Please provide OpenAI API Key and press ✨ Start New Chat."
26
+ )
27
 
28
+ with gr.Row(equal_height=True):
29
+ msg = gr.Textbox(
30
+ placeholder="Type a message...",
31
+ show_label=False
32
+ )
33
+ submit_btn = gr.Button("Submit", scale=0)
34
+
35
+ msg.submit(chat_send_user_answer, [msg, chatbot], [msg, chatbot], queue=False).then(
36
+ chat_send_assistant_answer, chatbot, chatbot
37
+ )
38
+
39
+ submit_btn.click(chat_send_user_answer, [msg, chatbot], [msg, chatbot], queue=False).then(
40
+ chat_send_assistant_answer, chatbot, chatbot
41
+ )
42
+
43
+ start_new_chat_btn.click(chat_clear_history, outputs=[chatbot], queue=False).then(
44
+ chat_start_new, outputs=[chatbot]
45
+ )
46
+
47
+ # Settings interface.
48
+ with gr.Blocks() as settings_app:
49
+ def update_chat_length_state(choice, state):
50
+ state = choice
51
+ set_chat_length(choice)
52
+ return state
53
+
54
+ def update_show_en_translation_state(value, state):
55
+ state = value
56
+ set_show_en_translation(value)
57
+ return state
58
 
59
+ with gr.Blocks() as demo:
60
+ chat_length_state = gr.State()
61
+ show_en_translation_state = gr.State()
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ with gr.Row():
64
+ with gr.Column():
65
+ chat_length_radio = gr.Radio(
66
+ [ChatLength.SHORT.value, ChatLength.MEDIUM.value, ChatLength.LONG.value],
67
+ show_label=False,
68
+ info="Chat length",
69
+ value=get_chat_length()
70
+ )
71
+ with gr.Column():
72
+ show_en_translation_checkbox = gr.Checkbox(
73
+ label="Show English translation",
74
+ value=get_show_en_translation()
75
+ )
76
+
77
+ gr.Markdown("Please start a new chat after changing any settings.")
78
+
79
+ chat_length_radio.change(
80
+ update_chat_length_state,
81
+ [chat_length_radio, chat_length_state],
82
+ chat_length_state
83
+ )
84
+ show_en_translation_checkbox.change(
85
+ update_show_en_translation_state,
86
+ [show_en_translation_checkbox, show_en_translation_state],
87
+ show_en_translation_state
88
+ )
89
+
90
+ # Tabs for navigation.
91
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
92
+ with gr.Tab("Chat"):
93
+ chat_app.render()
94
+ with gr.Tab("Settings"):
95
+ settings_app.render()
96
 
97
  app.launch()