Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,24 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from diffusers import DiffusionPipeline, FluxPipeline
|
| 4 |
import torch
|
| 5 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
device = "cpu"
|
| 8 |
# Load models
|
| 9 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-dra-en")
|
| 10 |
-
summarizer = pipeline("summarization", model="Falconsai/text_summarization")
|
| 11 |
-
# image_pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to(device)
|
| 12 |
-
# image_pipe = fluxPipeline("image-generation", model=DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to(device))
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
# Functions for each task
|
|
@@ -25,11 +29,36 @@ def translate_tamil_to_english(text):
|
|
| 25 |
|
| 26 |
def summarize_english_text(paragraph):
|
| 27 |
time.sleep(2)
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def english_text_to_image(text):
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return image
|
| 34 |
|
| 35 |
# Custom CSS
|
|
@@ -93,27 +122,79 @@ st.markdown("""
|
|
| 93 |
</style>
|
| 94 |
""", unsafe_allow_html=True)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
+
import requests
|
| 6 |
+
import io
|
| 7 |
+
import os
|
| 8 |
+
from PIL import Image
|
| 9 |
|
|
|
|
| 10 |
# Load models
|
| 11 |
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-dra-en")
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# for summarizer api
|
| 14 |
+
SUMMARIZER_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 15 |
+
summarizer_headers = {"Authorization": f"Bearer {os.getenv('GROQ_API_TOKEN')}",
|
| 16 |
+
"Content-Type": "application/json"}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# for image api
|
| 20 |
+
IMAGE_API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
|
| 21 |
+
img_headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
|
| 22 |
|
| 23 |
|
| 24 |
# Functions for each task
|
|
|
|
| 29 |
|
| 30 |
def summarize_english_text(paragraph):
|
| 31 |
time.sleep(2)
|
| 32 |
+
# Request payload
|
| 33 |
+
payload = {
|
| 34 |
+
"model": "mixtral-8x7b-32768",
|
| 35 |
+
"messages": [
|
| 36 |
+
{"role": "system", "content": "Create a summary of below paragraph in 30 words max"},
|
| 37 |
+
{"role": "user", "content": paragraph}
|
| 38 |
+
],
|
| 39 |
+
"max_tokens": 100 # number of words in the output.
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# Send POST request to Groq API
|
| 43 |
+
response = requests.post(SUMMARIZER_API_URL, json=payload, headers=summarizer_headers)
|
| 44 |
+
|
| 45 |
+
# Check if the request was successful
|
| 46 |
+
if response.status_code == 200:
|
| 47 |
+
# Parse the JSON response
|
| 48 |
+
result = response.json()
|
| 49 |
+
# Extract and print the generated text
|
| 50 |
+
generated_text = result['choices'][0]['message']['content']
|
| 51 |
+
return generated_text
|
| 52 |
+
else:
|
| 53 |
+
return f"Error: {response.status_code}, {response.text}"
|
| 54 |
|
| 55 |
def english_text_to_image(text):
|
| 56 |
+
payload = {
|
| 57 |
+
"inputs": prompt,
|
| 58 |
+
}
|
| 59 |
+
response = requests.post(IMAGE_API_URL, headers=img_headers, json=payload)
|
| 60 |
+
image_bytes = response.content
|
| 61 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 62 |
return image
|
| 63 |
|
| 64 |
# Custom CSS
|
|
|
|
| 122 |
</style>
|
| 123 |
""", unsafe_allow_html=True)
|
| 124 |
|
| 125 |
+
|
| 126 |
+
#sidebar styling
|
| 127 |
+
st.markdown("""
|
| 128 |
+
<style>
|
| 129 |
+
[data-testid=stSidebar] {
|
| 130 |
+
background-color: #FFFFFF;
|
| 131 |
+
margin-right: 20px;
|
| 132 |
+
border-right: 2px solid #FFFFFF
|
| 133 |
+
}
|
| 134 |
+
</style>
|
| 135 |
+
""", unsafe_allow_html=True)
|
| 136 |
+
|
| 137 |
+
#options styling in sidebar and added image in sidebar
|
| 138 |
+
with st.sidebar:
|
| 139 |
+
selected = option_menu(
|
| 140 |
+
menu_title="",
|
| 141 |
+
options=['Home','Tool'],
|
| 142 |
+
icons=['house-door-fill','setting'],
|
| 143 |
+
menu_icon='truck-front-fill',
|
| 144 |
+
default_index=0,
|
| 145 |
+
styles={
|
| 146 |
+
"container": {'padding':'5!important','background-color':'#FAF9F6'},
|
| 147 |
+
"icon": {'color':"#000000", "font-size":"23px"},
|
| 148 |
+
"nav-link": {'font-size':'16px','text-align':'left','margin':'0px','--hover-color':'#EDEADE','font-weight':'bold'},
|
| 149 |
+
"nav-link-selector":{'background-color':'#E6E6FA','font-weight':'bold'}
|
| 150 |
+
}
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
if selected == "Home":
|
| 155 |
+
# Page title and header
|
| 156 |
+
st.title(":blue[Multi-Purpose Tool] - Empowering Educators 🎓")
|
| 157 |
+
|
| 158 |
+
# Subheader for the app description
|
| 159 |
+
st.subheader("A versatile tool designed to assist teachers in translating, summarizing, and visualizing concepts.")
|
| 160 |
+
|
| 161 |
+
# Main description with detailed information about the app
|
| 162 |
+
st.markdown("""
|
| 163 |
+
The **Multi-Purpose Tool** is a user-friendly platform developed for educators,
|
| 164 |
+
enabling them to enhance their teaching experience. Whether it's translating content
|
| 165 |
+
into different languages, summarizing lengthy materials, or visualizing concepts
|
| 166 |
+
through images, this tool provides a one-stop solution for modern teaching needs.
|
| 167 |
+
|
| 168 |
+
### Key Features:
|
| 169 |
+
- **Translation**: Translate text seamlessly between languages (e.g., Tamil to English).
|
| 170 |
+
- **Summarization**: Quickly generate summaries of long passages for easy understanding.
|
| 171 |
+
- **Text to Image**: Visualize difficult concepts by generating images from text descriptions.
|
| 172 |
+
|
| 173 |
+
### Available Worldwide:
|
| 174 |
+
The Multi-Purpose Tool is deployed on Hugging Face and accessible globally to teachers
|
| 175 |
+
and educators at the click of a button. Visit the [app here](https://huggingface.co/spaces/Jesivn/Multi_Purpose_Tool).
|
| 176 |
+
|
| 177 |
+
Empower your classroom with advanced AI tools today!
|
| 178 |
+
""")
|
| 179 |
+
elif selected=="Tool":
|
| 180 |
+
# Row 1: Tamil to English translation
|
| 181 |
+
st.subheader("🌐 Translate Tamil to English")
|
| 182 |
+
tamil_input = st.text_area("Enter Tamil text", "")
|
| 183 |
+
if st.button("Translate"):
|
| 184 |
+
english_output = translate_tamil_to_english(tamil_input)
|
| 185 |
+
st.markdown(f"**Translated English Text**: {english_output}")
|
| 186 |
+
|
| 187 |
+
# Row 2: English paragraph summarization
|
| 188 |
+
st.subheader("📝 Summarize English Paragraph")
|
| 189 |
+
english_paragraph = st.text_area("Enter English paragraph", "")
|
| 190 |
+
if st.button("Summarize"):
|
| 191 |
+
summary_output = summarize_english_text(english_paragraph)
|
| 192 |
+
st.markdown(f"**Summary**: {summary_output}")
|
| 193 |
+
|
| 194 |
+
# Row 3: English text to image generation
|
| 195 |
+
st.subheader("🎨 Generate Image from English Text")
|
| 196 |
+
image_text = st.text_input("Enter description for image generation", "")
|
| 197 |
+
if st.button("Generate Image"):
|
| 198 |
+
generated_image = english_text_to_image(image_text)
|
| 199 |
+
st.image(generated_image, caption="Generated Image")
|
| 200 |
|