mobenta commited on
Commit
1b5a143
·
verified ·
1 Parent(s): 56d1631

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +175 -0
app.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import concurrent.futures as cf
2
+ import glob
3
+ import io
4
+ import os
5
+ import time
6
+ from pathlib import Path
7
+ from tempfile import NamedTemporaryFile
8
+ from typing import List, Literal
9
+ import re
10
+ from transformers import pipeline
11
+ from pydantic import BaseModel
12
+
13
+ # Initialize Hugging Face text generation model
14
+ text_generator = pipeline('text-generation', model='EleutherAI/gpt-neo-2.7B')
15
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
16
+
17
+ # Instruction templates (unchanged from your original code)
18
+ INSTRUCTION_TEMPLATES = {
19
+ "podcast": {
20
+ "intro": """Your task is to take the input text provided and turn it into a lively, engaging, informative podcast dialogue, in the style of NPR...""",
21
+ "text_instructions": "First, carefully read through the input text...",
22
+ "scratch_pad": """Brainstorm creative ways to discuss the main topics...""",
23
+ "prelude": """Now that you have brainstormed ideas and created a rough outline...""",
24
+ "dialog": """Write a very long, engaging, informative podcast dialogue..."""
25
+ }
26
+ }
27
+
28
+ # Function to update instruction fields based on template selection
29
+ def update_instructions(template):
30
+ return (
31
+ INSTRUCTION_TEMPLATES[template]["intro"],
32
+ INSTRUCTION_TEMPLATES[template]["text_instructions"],
33
+ INSTRUCTION_TEMPLATES[template]["scratch_pad"],
34
+ INSTRUCTION_TEMPLATES[template]["prelude"],
35
+ INSTRUCTION_TEMPLATES[template]["dialog"]
36
+ )
37
+
38
+ # Define the structure of dialogue
39
+ class DialogueItem(BaseModel):
40
+ text: str
41
+ speaker: Literal["speaker-1", "speaker-2"]
42
+
43
+ class Dialogue(BaseModel):
44
+ scratchpad: str
45
+ dialogue: List[DialogueItem]
46
+
47
+ # Function to read README.md
48
+ def read_readme():
49
+ readme_path = Path("README.md")
50
+ if readme_path.exists():
51
+ with open(readme_path, "r") as file:
52
+ content = file.read()
53
+ content = re.sub(r'--.*?--', '', content, flags=re.DOTALL)
54
+ return content
55
+ else:
56
+ return "README.md not found. Please check the repository for more information."
57
+
58
+ # Hugging Face-based dialogue generation function
59
+ def generate_dialogue(text: str, intro_instructions: str, text_instructions: str,
60
+ scratch_pad_instructions: str, prelude_dialog: str,
61
+ podcast_dialog_instructions: str, edited_transcript: str = None,
62
+ user_feedback: str = None) -> str:
63
+ # Combine instructions and text into a prompt
64
+ full_prompt = f"""
65
+ {intro_instructions}
66
+
67
+ Original text:
68
+ {text}
69
+
70
+ {text_instructions}
71
+
72
+ Brainstorming:
73
+ {scratch_pad_instructions}
74
+
75
+ Prelude:
76
+ {prelude_dialog}
77
+
78
+ Dialogue:
79
+ {podcast_dialog_instructions}
80
+
81
+ {edited_transcript if edited_transcript else ""}
82
+ {user_feedback if user_feedback else ""}
83
+ """
84
+
85
+ # Generate text using Hugging Face model
86
+ generated = text_generator(full_prompt, max_length=1000) # Adjust max_length as needed
87
+ return generated[0]['generated_text'] # Extract generated text from the response
88
+
89
+ # Function to handle audio generation (could be expanded later)
90
+ def get_mp3(text: str, voice: str, audio_model: str) -> bytes:
91
+ # Placeholder for audio generation; currently not implemented
92
+ # You can use text-to-speech services or local TTS engines
93
+ return b""
94
+
95
+ # Main audio generation function (adapted for Hugging Face text generation)
96
+ def generate_audio(
97
+ files: list,
98
+ text_model: str = "EleutherAI/gpt-neo-2.7B",
99
+ audio_model: str = "tts-1",
100
+ speaker_1_voice: str = "alloy",
101
+ speaker_2_voice: str = "echo",
102
+ intro_instructions: str = '',
103
+ text_instructions: str = '',
104
+ scratch_pad_instructions: str = '',
105
+ prelude_dialog: str = '',
106
+ podcast_dialog_instructions: str = '',
107
+ edited_transcript: str = None,
108
+ user_feedback: str = None,
109
+ original_text: str = None,
110
+ debug = False,
111
+ ) -> tuple:
112
+
113
+ # Combine input text from files
114
+ combined_text = original_text or ""
115
+ if not combined_text:
116
+ for file in files:
117
+ with Path(file).open("rb") as f:
118
+ text = f.read().decode('utf-8') # Assuming the PDF text is extracted as UTF-8
119
+ combined_text += text + "\n\n"
120
+
121
+ # Generate the dialogue using Hugging Face
122
+ llm_output = generate_dialogue(
123
+ combined_text,
124
+ intro_instructions=intro_instructions,
125
+ text_instructions=text_instructions,
126
+ scratch_pad_instructions=scratch_pad_instructions,
127
+ prelude_dialog=prelude_dialog,
128
+ podcast_dialog_instructions=podcast_dialog_instructions,
129
+ edited_transcript=edited_transcript,
130
+ user_feedback=user_feedback
131
+ )
132
+
133
+ # Placeholder for audio (since TTS implementation is omitted)
134
+ audio = b""
135
+ transcript = llm_output
136
+ characters = len(llm_output)
137
+
138
+ # Generating audio (placeholder logic)
139
+ with cf.ThreadPoolExecutor() as executor:
140
+ futures = []
141
+ for line in llm_output.split('\n'):
142
+ future = executor.submit(get_mp3, line, speaker_1_voice, audio_model)
143
+ futures.append(future)
144
+ characters += len(line)
145
+
146
+ for future in futures:
147
+ audio_chunk = future.result()
148
+ audio += audio_chunk
149
+
150
+ temporary_directory = "./tmp/"
151
+ os.makedirs(temporary_directory, exist_ok=True)
152
+
153
+ # Save audio to a temporary file
154
+ temporary_file = NamedTemporaryFile(dir=temporary_directory, delete=False, suffix=".mp3")
155
+ temporary_file.write(audio)
156
+ temporary_file.close()
157
+
158
+ return temporary_file.name, transcript, combined_text
159
+
160
+ # Example call to generate audio
161
+ files = ["sample.pdf"] # Replace with your actual PDF file paths
162
+ audio_file, transcript, original_text = generate_audio(
163
+ files=files,
164
+ intro_instructions="Your task is to create a podcast...",
165
+ text_instructions="Extract the main points...",
166
+ scratch_pad_instructions="Brainstorm how to present the topics...",
167
+ prelude_dialog="Now let's write the podcast dialogue...",
168
+ podcast_dialog_instructions="Write a long and engaging podcast dialogue."
169
+ )
170
+
171
+ # Print output transcript (or save it as needed)
172
+ print(transcript)
173
+
174
+ # Read and print README content
175
+ print(read_readme())