Sad44587 commited on
Commit
a66075a
·
verified ·
1 Parent(s): a37b0a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -49
app.py CHANGED
@@ -1,64 +1,107 @@
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 asyncio
3
+ import tempfile
4
+ import edge_tts
5
  from huggingface_hub import InferenceClient
6
 
7
+ # -------------------------
8
+ # === Fast Chat (Gemma) ===
9
+ # -------------------------
10
+ client_fast = InferenceClient("google/gemma-1.1-2b-it")
11
 
12
+ def fast_chat(query):
13
+ messages = [{
14
+ "role": "user",
15
+ "content": f"[SYSTEM] You are ASSISTANT who answers questions in a short and concise manner. [USER] {query}"
16
+ }]
17
+ response = ""
18
+ for message in client_fast.chat_completion(messages, max_tokens=2048, stream=True):
19
+ token = message.choices[0].delta.content
20
+ response += token
21
+ yield response
22
 
23
+ # -----------------------------
24
+ # === Critical Thinker Chat ===
25
+ # -----------------------------
26
+ client_critical = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
 
 
 
 
 
27
 
28
+ def critical_thinker(query):
29
+ budget = 10
30
+ prompt = f"""[INST] [SYSTEM] You are a French robot full of hope and enthusiasm for future projects...
31
+ <QUERY> {query} [/INST] [ASSISTANT]"""
32
+ stream = client_critical.text_generation(prompt, max_new_tokens=4096, stream=True, details=True, return_full_text=False)
33
+ output = ""
34
+ for response in stream:
35
+ output += response.token.text
36
+ return output
37
 
38
+ # -----------------
39
+ # === Edge TTS ====
40
+ # -----------------
41
+ async def get_voices():
42
+ voices = await edge_tts.list_voices()
43
+ return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
44
 
45
+ async def text_to_speech(text, voice, rate, pitch):
46
+ if not text.strip():
47
+ return None, gr.Warning("Please enter text to convert.")
48
+ if not voice:
49
+ return None, gr.Warning("Please select a voice.")
50
+
51
+ voice_short_name = voice.split(" - ")[0]
52
+ rate_str = f"{rate:+d}%"
53
+ pitch_str = f"{pitch:+d}Hz"
54
+ communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
55
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
56
+ tmp_path = tmp_file.name
57
+ await communicate.save(tmp_path)
58
+ return tmp_path, None
59
 
60
+ def tts_interface(text, voice, rate, pitch):
61
+ audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
62
+ return audio, warning
 
 
 
 
 
63
 
64
+ # -------------------------------
65
+ # === Interface Gradio globale ==
66
+ # -------------------------------
67
+ async def main():
68
+ voices = await get_voices()
69
+
70
+ # Onglet 1 : Chat Rapide
71
+ with gr.Blocks() as fast_tab:
72
+ gr.Markdown("# 💬 Fast Chat")
73
+ gr.Interface(fn=fast_chat, inputs=gr.Textbox(label="Your message"), outputs="text")
74
+
75
+ # Onglet 2 : Chat Critique
76
+ with gr.Blocks() as critical_tab:
77
+ gr.Markdown("# 🤖 Critical Thinker")
78
+ gr.Interface(fn=critical_thinker, inputs=gr.Textbox(label="Your question"), outputs="text")
79
 
80
+ # Onglet 3 : TTS
81
+ with gr.Blocks() as tts_tab:
82
+ gr.Markdown("# 🔊 Text-to-Speech")
83
+ gr.Interface(
84
+ fn=tts_interface,
85
+ inputs=[
86
+ gr.Textbox(label="Text to Speak", lines=5),
87
+ gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice"),
88
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Rate (%)"),
89
+ gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch (Hz)")
90
+ ],
91
+ outputs=[
92
+ gr.Audio(label="Generated Audio", type="filepath"),
93
+ gr.Markdown(label="Warning", visible=False)
94
+ ]
95
+ )
96
 
97
+ # Tabs Fusionnées
98
+ app = gr.TabbedInterface(
99
+ interface_list=[fast_tab, critical_tab, tts_tab],
100
+ tab_names=["Fast Chat", "Critical Thinker", "TTS"]
101
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
+ app.queue()
104
+ app.launch()
105
 
106
  if __name__ == "__main__":
107
+ asyncio.run(main())