Sad44587 commited on
Commit
aec2dde
·
verified ·
1 Parent(s): 7ac4dca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -52
app.py CHANGED
@@ -1,75 +1,66 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
3
 
 
4
  client = InferenceClient("google/gemma-1.1-2b-it")
5
- client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
6
 
7
- def models(Query):
8
-
9
- messages = []
 
 
 
 
 
 
 
 
 
 
10
 
11
- messages.append({"role": "user", "content": f"[SYSTEM] You are ASSISTANT who answer question asked by user in short and concise manner. [USER] {Query}"})
12
 
13
- Response = ""
 
 
 
14
 
15
- for message in client.chat_completion(
16
- messages,
17
- max_tokens=2048,
18
- stream=True
19
- ):
20
  token = message.choices[0].delta.content
 
21
 
22
- Response += token
23
- yield Response
 
24
 
 
25
  def nemo(query):
26
  budget = 3
27
- message = f"""[INST] [SYSTEM] You are a helpful french assistant in normal conversation.
28
- When given a problem to solve, you are an expert problem-solving assistant.
29
- Your task is to provide a detailed, step-by-step solution to a given question.
30
- Follow these instructions carefully:
31
- 1. Read the given question carefully and reset counter between <count> and </count> to {budget} (maximum 3 steps).
32
- 2. Think critically like a human researcher or scientist. Break down the problem using first principles to conceptually understand and answer the question.
33
- 3. Generate a detailed, logical step-by-step solution.
34
- 4. Enclose each step of your solution within <step> and </step> tags.
35
- 5. You are allowed to use at most {budget} steps (starting budget), keep track of it by counting down within tags <count> </count>, STOP GENERATING MORE STEPS when hitting 0, you don't have to use all of them.
36
- 6. Do a self-reflection when you are unsure about how to proceed, based on the self-reflection and reward, decide whether you need to return to the previous steps.
37
- 7. After completing the solution steps, reorganize and synthesize the steps into the final answer within <answer> and </answer> tags.
38
- 8. Provide a critical, honest, and subjective self-evaluation of your reasoning process within <reflection> and </reflection> tags.
39
- 9. Assign a quality score to your solution as a float between 0.0 (lowest quality) and 1.0 (highest quality), enclosed in <reward> and </reward> tags.
40
- Example format:
41
- <count> [starting budget] </count>
42
- <step> [Content of step 1] </step>
43
- <count> [remaining budget] </count>
44
- <step> [Content of step 2] </step>
45
- <reflection> [Evaluation of the steps so far] </reflection>
46
- <reward> [Float between 0.0 and 1.0] </reward>
47
- <count> [remaining budget] </count>
48
- <step> [Content of step 3 or Content of some previous step] </step>
49
- <count> [remaining budget] </count>
50
- ...
51
- <step> [Content of final step] </step>
52
- <count> [remaining budget] </count>
53
- <answer> [Final Answer] </answer> (must give final answer in this format)
54
- <reflection> [Evaluation of the solution] </reflection>
55
- <reward> [Float between 0.0 and 1.0] </reward> [/INST] [INST] [QUERY] {query} [/INST] [ASSISTANT] """
56
 
57
- stream = client.text_generation(message, max_new_tokens=4096, stream=True, details=True, return_full_text=False)
58
- output = ""
59
 
60
- for response in stream:
61
- output += response.token.text
62
- return output
63
 
64
- description="# Light ChatBox\n### Enter a question and.. Tada this reponse generate in 0.5 second!"
 
65
 
66
  with gr.Blocks() as demo1:
67
- gr.Interface(description=description,fn=models, inputs=["text"], outputs="text")
 
68
  with gr.Blocks() as demo2:
69
- gr.Interface(description="Very low but critical thinker",fn=nemo, inputs=["text"], outputs="text", api_name="critical_thinker", concurrency_limit=10)
70
 
71
  with gr.Blocks() as demo:
72
- gr.TabbedInterface([demo1, demo2] , ["Fast", "Critical"])
73
 
74
- demo.queue(max_size=300000)
75
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import edge_tts
4
+ import tempfile
5
+ import asyncio
6
 
7
+ # Client Hugging Face
8
  client = InferenceClient("google/gemma-1.1-2b-it")
 
9
 
10
+ # Fonction de synthèse vocale (TTS)
11
+ async def text_to_speech(text, voice="fr-FR-DeniseNeural", rate=0, pitch=0):
12
+ if not text.strip():
13
+ return None, "Veuillez entrer du texte."
14
+
15
+ rate_str = f"{rate:+d}%"
16
+ pitch_str = f"{pitch:+d}Hz"
17
+ communicate = edge_tts.Communicate(text, voice, rate=rate_str, pitch=pitch_str)
18
+
19
+ # Sauvegarde en fichier temporaire
20
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
21
+ tmp_path = tmp_file.name
22
+ await communicate.save(tmp_path)
23
 
24
+ return tmp_path
25
 
26
+ # Modèle rapide (Fast)
27
+ def models(query):
28
+ messages = [{"role": "user", "content": f"[SYSTEM] You are a fast assistant. [USER] {query}"}]
29
+ response = ""
30
 
31
+ for message in client.chat_completion(messages, max_tokens=2048, stream=True):
 
 
 
 
32
  token = message.choices[0].delta.content
33
+ response += token
34
 
35
+ # Convertir en audio
36
+ tts_path = asyncio.run(text_to_speech(response))
37
+ return response, tts_path
38
 
39
+ # Modèle critique (Critical Thinker)
40
  def nemo(query):
41
  budget = 3
42
+ message = f"""[INST] [SYSTEM] You are a deep-thinking assistant.
43
+ <count> {budget} </count> <step> Analyzing question... </step> <count> {budget-1} </count>
44
+ <answer> Here is your answer: {query} </answer> [/INST]"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ stream = client.text_generation(message, max_new_tokens=4096, stream=True)
47
+ output = "".join([response.token.text for response in stream])
48
 
49
+ # Convertir en audio
50
+ tts_path = asyncio.run(text_to_speech(output))
51
+ return output, tts_path
52
 
53
+ # Interface Gradio
54
+ description = "# Light ChatBox\n### Enter a question and get a response with voice!"
55
 
56
  with gr.Blocks() as demo1:
57
+ gr.Interface(fn=models, inputs="text", outputs=["text", "audio"], description=description)
58
+
59
  with gr.Blocks() as demo2:
60
+ gr.Interface(fn=nemo, inputs="text", outputs=["text", "audio"], description="Critical Thinker")
61
 
62
  with gr.Blocks() as demo:
63
+ gr.TabbedInterface([demo1, demo2], ["Fast", "Critical"])
64
 
65
+ demo.queue()
66
  demo.launch()