Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,131 +1,47 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
-
|
4 |
-
from transformers import pipeline
|
5 |
-
from langchain.prompts import ChatPromptTemplate
|
6 |
from io import BytesIO
|
7 |
-
import time
|
8 |
-
import wandb
|
9 |
-
from rouge import Rouge
|
10 |
|
11 |
-
# Environment
|
12 |
-
os.environ['TOGETHER_API_KEY'] = '
|
13 |
-
os.environ['PINECONE_API_KEY'] = '
|
14 |
-
ELEVENLABS_API_KEY = 'your_elevenlabs_api_key'
|
15 |
|
16 |
-
#
|
17 |
-
summary_prompt = """
|
18 |
-
You are an expert AI summarization model tasked with creating a comprehensive summary for 10 years old kids of the provided context. The summary should be approximately one page long and well-structured.
|
19 |
|
20 |
-
|
21 |
-
```{context}```
|
22 |
-
|
23 |
-
Please follow these specific guidelines for the summary:
|
24 |
-
|
25 |
-
### Detailed Summary
|
26 |
-
- **Section 1: Key Concepts**
|
27 |
-
- Introduce the first major topic or theme.
|
28 |
-
- Use bullet points to list important details and insights.
|
29 |
-
|
30 |
-
- **Section 2: Supporting Details**
|
31 |
-
- Discuss secondary topics or supporting arguments.
|
32 |
-
- Use bullet points to outline critical information and findings.
|
33 |
-
|
34 |
-
### Conclusion
|
35 |
-
- Suggest any potential actions, solutions, or recommendations.
|
36 |
-
|
37 |
-
this is the summary:
|
38 |
-
"""
|
39 |
-
summary_prompt_template = ChatPromptTemplate.from_template(summary_prompt)
|
40 |
-
|
41 |
-
# Define the PDF extraction function
|
42 |
-
def extract_text_from_pdf(file):
|
43 |
-
reader = PdfReader(file)
|
44 |
-
text = ""
|
45 |
-
for page in reader.pages:
|
46 |
-
text += page.extract_text()
|
47 |
-
return text
|
48 |
-
|
49 |
-
# Define the text-to-speech function
|
50 |
-
def text_to_speech_stream(text):
|
51 |
-
client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
52 |
-
response = client.text_to_speech.convert(
|
53 |
-
voice_id="jBpfuIE2acCO8z3wKNLl",
|
54 |
-
optimize_streaming_latency="0",
|
55 |
-
output_format="mp3_44100_64",
|
56 |
-
text=text,
|
57 |
-
model_id="eleven_multilingual_v2",
|
58 |
-
voice_settings=VoiceSettings(
|
59 |
-
stability=0.5,
|
60 |
-
similarity_boost=0.75,
|
61 |
-
style=0,
|
62 |
-
use_speaker_boost=True,
|
63 |
-
),
|
64 |
-
)
|
65 |
-
|
66 |
-
audio_data = BytesIO()
|
67 |
-
for chunk in response:
|
68 |
-
if chunk:
|
69 |
-
audio_data.write(chunk)
|
70 |
-
|
71 |
-
audio_data.seek(0)
|
72 |
-
if not os.path.exists('samples'):
|
73 |
-
os.makedirs('samples')
|
74 |
-
|
75 |
-
with open('samples/output.mp3', 'wb') as f:
|
76 |
-
f.write(audio_data.read())
|
77 |
-
|
78 |
-
return 'samples/output.mp3'
|
79 |
-
|
80 |
-
# Define the evaluation function
|
81 |
-
def evaluate_summary(generated_summary):
|
82 |
-
wandb.init(project="learnverse")
|
83 |
-
|
84 |
-
reference_summaries = ["Reference summary 1...", "Reference summary 2...", "Reference summary 3..."]
|
85 |
-
rouge = Rouge()
|
86 |
-
|
87 |
-
rouge_1, rouge_2, rouge_l = {'r': 0, 'p': 0, 'f': 0}, {'r': 0, 'p': 0, 'f': 0}, {'r': 0, 'p': 0, 'f': 0}
|
88 |
-
|
89 |
-
for reference in reference_summaries:
|
90 |
-
scores = rouge.get_scores(generated_summary, reference)
|
91 |
-
rouge_1['r'] += scores[0]['rouge-1']['r']
|
92 |
-
rouge_1['p'] += scores[0]['rouge-1']['p']
|
93 |
-
rouge_1['f'] += scores[0]['rouge-1']['f']
|
94 |
-
rouge_2['r'] += scores[0]['rouge-2']['r']
|
95 |
-
rouge_2['p'] += scores[0]['rouge-2']['p']
|
96 |
-
rouge_2['f'] += scores[0]['rouge-2']['f']
|
97 |
-
rouge_l['r'] += scores[0]['rouge-l']['r']
|
98 |
-
rouge_l['p'] += scores[0]['rouge-l']['p']
|
99 |
-
rouge_l['f'] += scores[0]['rouge-l']['f']
|
100 |
-
|
101 |
-
num_references = len(reference_summaries)
|
102 |
-
rouge_1 = {key: value / num_references for key, value in rouge_1.items()}
|
103 |
-
rouge_2 = {key: value / num_references for key, value in rouge_2.items()}
|
104 |
-
rouge_l = {key: value / num_references for key, value in rouge_l.items()}
|
105 |
-
|
106 |
-
wandb.log(rouge_1)
|
107 |
-
wandb.log(rouge_2)
|
108 |
-
wandb.log(rouge_l)
|
109 |
-
wandb.finish()
|
110 |
-
|
111 |
-
return {'ROUGE-1': rouge_1, 'ROUGE-2': rouge_2, 'ROUGE-L': rouge_l}
|
112 |
-
|
113 |
-
# Define the main processing function
|
114 |
def process_question(file):
|
|
|
115 |
pdffile = extract_text_from_pdf(file)
|
116 |
-
|
|
|
117 |
evaluation = evaluate_summary(summary)
|
118 |
audio_file = text_to_speech_stream(summary)
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
gr.
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain_together import ChatTogether
|
3 |
+
from langchain_community.llms import Together
|
4 |
+
from langchain_pinecone import PineconeVectorStore
|
5 |
+
from langchain_openai import OpenAIEmbeddings
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain_core.output_parsers import StrOutputParser
|
8 |
+
from langchain_community.document_loaders import PyPDFLoader
|
9 |
+
from elevenlabs.client import ElevenLabs
|
10 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
11 |
import os
|
12 |
+
import torch
|
|
|
|
|
13 |
from io import BytesIO
|
|
|
|
|
|
|
14 |
|
15 |
+
# Environment Variables
|
16 |
+
os.environ['TOGETHER_API_KEY'] = 'e83925ff068ab5e4598a56f68385fd37144469f50eec94f5c2e6647798f1be9e'
|
17 |
+
os.environ['PINECONE_API_KEY'] = 'f7413055-9b13-4bbc-8c92-56132e034bff'
|
|
|
18 |
|
19 |
+
# Define your functions and classes here based on your original code
|
|
|
|
|
20 |
|
21 |
+
# Example function to process questions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def process_question(file):
|
23 |
+
# Use your existing functions to process the PDF and generate outputs
|
24 |
pdffile = extract_text_from_pdf(file)
|
25 |
+
three_topics = topic_chain.invoke({"context": pdffile})
|
26 |
+
summary = summary_pdf_chain.invoke(pdffile)
|
27 |
evaluation = evaluate_summary(summary)
|
28 |
audio_file = text_to_speech_stream(summary)
|
29 |
+
prompt = topics_prompt
|
30 |
+
shape = generate_gif(prompt)
|
31 |
+
ai_asistant = animate_image(audio_file)
|
32 |
+
return summary, evaluation, ai_asistant, shape
|
33 |
+
|
34 |
+
# Define Gradio Interface
|
35 |
+
iface = gr.Interface(
|
36 |
+
fn=process_question,
|
37 |
+
inputs=gr.inputs.File(label="Upload PDF File"),
|
38 |
+
outputs=[
|
39 |
+
gr.outputs.Textbox(label="Summary"),
|
40 |
+
gr.outputs.Textbox(label="Evaluation"),
|
41 |
+
gr.outputs.Audio(label="AI Assistant"),
|
42 |
+
gr.outputs.Image(label="3D Shape")
|
43 |
+
]
|
44 |
+
)
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
iface.launch()
|