aigorithm commited on
Commit
672e79a
Β·
verified Β·
1 Parent(s): c907369

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +111 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import requests
4
+ import os
5
+ import html
6
+ import base64
7
+ import random
8
+ import json
9
+ from datetime import datetime
10
+
11
+ chat_gen = pipeline("text-generation", model="tiiuae/falcon-rw-1b")
12
+
13
+ voice_options = {
14
+ "Rachel": "EXAVITQu4vr4xnSDxMaL",
15
+ "Adam": "21m00Tcm4TlvDq8ikWAM",
16
+ "Elli": "AZnzlk1XvdvUeBnXmlld",
17
+ "Josh": "VR6AewLTigWG4xSOukaG"
18
+ }
19
+
20
+ DEFAULT_ELEVEN_API_KEY = "sk_4e67c39c0e9cbc87462cd2643e1f4d1d9959d7d81203adc2"
21
+ library_file = "/tmp/library.json"
22
+
23
+ def generate_chat_bundle(prompt, *character_inputs):
24
+ characters = [c.split(":")[0].strip() for c in character_inputs if ":" in c]
25
+ voices = {c.split(":")[0].strip(): c.split(":")[1].strip() for c in character_inputs if ":" in c}
26
+
27
+ system_prompt = (
28
+ f"Group chat between {', '.join(characters)}. Prompt: {prompt}. "
29
+ "Use casual texting style: Name: message"
30
+ )
31
+ response = chat_gen(system_prompt, max_new_tokens=300)[0]["generated_text"]
32
+ chat_lines = [(line.split(':', 1)[0].strip(), line.split(':', 1)[1].strip()) for line in response.splitlines() if ':' in line]
33
+
34
+ html_chat = ""
35
+ file_paths = []
36
+ api_key = os.getenv("ELEVEN_API_KEY", DEFAULT_ELEVEN_API_KEY)
37
+
38
+ for i, (name, msg) in enumerate(chat_lines):
39
+ color = random.choice(["#DCF8C6", "#FEE2E2", "#EDEDED", "#C7E2FF"])
40
+ html_chat += f"""<div style='margin: 10px; text-align: {'left' if i % 2 == 0 else 'right'}'>
41
+ <div style='display:inline-block; padding: 10px 15px; background: {color}; border-radius: 15px; max-width: 70%;'>
42
+ <strong>{html.escape(name)}:</strong> {html.escape(msg)}
43
+ </div></div>"""
44
+
45
+ voice_id = voices.get(name, list(voice_options.values())[0])
46
+ eleven_response = requests.post(
47
+ f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
48
+ headers={
49
+ "xi-api-key": api_key,
50
+ "Content-Type": "application/json"
51
+ },
52
+ json={"text": msg, "model_id": "eleven_monolingual_v1"}
53
+ )
54
+
55
+ if eleven_response.status_code == 200:
56
+ file_path = f"/tmp/audio_{i}_{name}.mp3"
57
+ with open(file_path, 'wb') as f:
58
+ f.write(eleven_response.content)
59
+ file_paths.append(file_path)
60
+
61
+ timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
62
+ html_path = f"/tmp/fakechat_{timestamp}.html"
63
+ with open(html_path, "w") as f:
64
+ f.write(f"<html><body>{html_chat}</body></html>")
65
+ file_paths.append(html_path)
66
+
67
+ if os.path.exists(library_file):
68
+ with open(library_file, "r") as f:
69
+ lib = json.load(f)
70
+ else:
71
+ lib = []
72
+
73
+ lib.insert(0, {
74
+ "timestamp": timestamp,
75
+ "characters": characters,
76
+ "prompt": prompt,
77
+ "html": html_path,
78
+ "audio_files": file_paths[:-1]
79
+ })
80
+
81
+ with open(library_file, "w") as f:
82
+ json.dump(lib, f, indent=2)
83
+
84
+ return file_paths
85
+
86
+ def get_library():
87
+ if not os.path.exists(library_file):
88
+ return "No stories saved yet."
89
+ with open(library_file, "r") as f:
90
+ lib = json.load(f)
91
+ html_links = [f"<li><b>{item['timestamp']}</b>: {', '.join(item['characters'])} β€” <a href='file={item['html']}' target='_blank'>Chat</a></li>" for item in lib]
92
+ return "<ul>" + "".join(html_links) + "</ul>"
93
+
94
+ def build_character_inputs():
95
+ return [gr.Textbox(label=f"Character {i+1} (Format: Name:Voice)", placeholder="e.g. Anna:Rachel") for i in range(4)]
96
+
97
+ with gr.Blocks() as app:
98
+ with gr.Row():
99
+ with gr.Column(scale=3):
100
+ characters = build_character_inputs()
101
+ prompt = gr.Textbox(label="Scene Prompt")
102
+ generate_btn = gr.Button("🎀 Generate Chat + Audio")
103
+ file_output = gr.File(file_types=[".html", ".mp3"], label="Download Files")
104
+ with gr.Column(scale=1):
105
+ lib_html = gr.HTML(label="πŸ“ My Saved Stories")
106
+ refresh_btn = gr.Button("πŸ” Refresh Library")
107
+
108
+ generate_btn.click(fn=generate_chat_bundle, inputs=[prompt] + characters, outputs=file_output)
109
+ refresh_btn.click(fn=get_library, outputs=lib_html)
110
+
111
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ gradio
3
+ torch
4
+ requests