asagasad commited on
Commit
ccf06cc
·
verified ·
1 Parent(s): ad62422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -51
app.py CHANGED
@@ -1,64 +1,64 @@
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
+ from gtts import gTTS
3
+ import tempfile
4
+ import os
5
+ import docx
6
+ from pydub import AudioSegment
7
 
8
+ # Function to extract text from docx
9
+ def extract_text_from_docx(file):
10
+ doc = docx.Document(file.name)
11
+ return [para.text.strip() for para in doc.paragraphs if para.text.strip()]
12
 
13
+ # Function to convert Urdu text to speech
14
+ def urdu_tts(text, speed):
15
+ tts = gTTS(text=text, lang='ur')
16
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
17
+ tts.save(tmp.name)
18
+ audio_path = tmp.name
19
 
20
+ # Change speed using pydub
21
+ sound = AudioSegment.from_file(audio_path)
22
+ sound = sound.speedup(playback_speed=speed)
23
+ final_path = audio_path.replace(".mp3", f"_x{speed}.mp3")
24
+ sound.export(final_path, format="mp3")
 
 
 
 
25
 
26
+ return final_path
 
 
 
 
27
 
28
+ # Gradio stateful interface
29
+ def start_reader(file, speed):
30
+ lines = extract_text_from_docx(file)
31
+ return lines, f"کل {len(lines)} لائنیں ملی ہیں۔", 0
32
 
33
+ def read_line(lines, line_idx, speed):
34
+ if line_idx < len(lines):
35
+ audio_file = urdu_tts(lines[line_idx], speed)
36
+ return lines[line_idx], audio_file, line_idx + 1
37
+ else:
38
+ return "📘 تمام لائنیں پڑھ لی گئیں!", None, line_idx
39
 
40
+ # Gradio UI
41
+ with gr.Blocks() as demo:
42
+ gr.Markdown("## 📖 اردو لائن بائی لائن ریڈر")
 
 
 
 
 
43
 
44
+ with gr.Row():
45
+ file_input = gr.File(label="📁 .docx فائل اپ لوڈ کریں", file_types=['.docx'])
46
+ speed_slider = gr.Slider(0.5, 2.0, 1.0, step=0.1, label="🔊 رفتار")
47
 
48
+ start_button = gr.Button("🚀 فائل پڑھنا شروع کریں")
49
+ status = gr.Textbox(label="🔹 اسٹیٹس")
50
+ lines_state = gr.State()
51
+ index_state = gr.State()
52
+
53
+ current_line = gr.Textbox(label="📌 موجودہ لائن", lines=2)
54
+ audio_output = gr.Audio(label="🎧 آڈیو")
55
 
56
+ next_button = gr.Button("▶️ اگلی لائن پڑھیں")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
+ start_button.click(fn=start_reader, inputs=[file_input, speed_slider],
59
+ outputs=[lines_state, status, index_state])
60
+
61
+ next_button.click(fn=read_line, inputs=[lines_state, index_state, speed_slider],
62
+ outputs=[current_line, audio_output, index_state])
63
 
64
+ demo.launch()