File size: 2,264 Bytes
afc7996
58ef530
8595152
 
 
 
 
 
afc7996
088c542
 
 
ab753b8
 
 
 
 
afc7996
ab753b8
8595152
 
 
 
ab753b8
 
 
 
 
 
 
 
 
 
 
 
afc7996
088c542
 
 
 
 
 
801be53
 
 
 
 
 
 
 
088c542
 
 
afc7996
088c542
401d95e
088c542
2c64b23
088c542
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
import re
import textstat
from transformers import pipeline
from langdetect import detect

# Load a summarization model
summarizer = pipeline("summarization")

# Load a text generation model from Hugging Face
text_generator = pipeline("text-generation", model="gpt2")

def text_analysis(text):
    # Analyze text: word count, character count, language detection, and readability
    words = re.findall(r'\w+', text.lower())
    sentences = re.split(r'[.!?]+', text)
    num_sentences = len(sentences) - 1
    num_words = len(words)
    num_chars = len("".join(words))
    reading_ease = textstat.flesch_reading_ease(text)
    language = detect(text)

    # Format the results
    return {
        "Language": language,
        "Sentences": num_sentences,
        "Words": num_words,
        "Characters": num_chars,
        "Readability (Flesch Reading Ease)": reading_ease
    }

def text_summarization(text):
    # Summarize text using the transformer model
    summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
    return summary

def generate_text(prompt):
    # Generate text using the loaded Hugging Face model
    generated_text = text_generator(prompt, max_length=50, num_return_sequences=1)[0]['generated_text']
    return generated_text

# Define interfaces for text analysis, text summarization, and text generation
text_analysis_interface = gr.Interface(fn=text_analysis, 
                                       inputs=gr.Textbox(lines=4, placeholder="Type something here..."), 
                                       outputs=gr.JSON(label="Text Analysis"))

text_summarization_interface = gr.Interface(fn=text_summarization, 
                                            inputs=gr.Textbox(lines=4, placeholder="Type something here..."), 
                                            outputs="text")

text_generation_interface = gr.Interface(fn=generate_text, 
                                         inputs=gr.Textbox(lines=4, placeholder="Type a prompt..."), 
                                         outputs="text")

# Launch the interfaces
if __name__ == "__main__":
    text_analysis_interface.launch()
    text_summarization_interface.launch()
    text_generation_interface.launch()