Jesivn's picture
Update app.py
2074259 verified
import streamlit as st
from streamlit_option_menu import option_menu
from transformers import pipeline
import torch
import time
import requests
import io
import os
from PIL import Image
# Load models
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-dra-en")
# for summarizer api
SUMMARIZER_API_URL = "https://api.groq.com/openai/v1/chat/completions"
summarizer_headers = {"Authorization": f"Bearer {os.getenv('GROQ_API_TOKEN')}",
"Content-Type": "application/json"}
# for image api
IMAGE_API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
img_headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
# Functions for each task
def translate_tamil_to_english(text):
time.sleep(2)
result = translator(text)
return result[0]['translation_text']
def summarize_english_text(paragraph):
time.sleep(2)
# Request payload
payload = {
"model": "mixtral-8x7b-32768",
"messages": [
{"role": "system", "content": "Create a summary of below paragraph in 30 words max"},
{"role": "user", "content": paragraph}
],
"max_tokens": 100 # number of words in the output.
}
# Send POST request to Groq API
response = requests.post(SUMMARIZER_API_URL, json=payload, headers=summarizer_headers)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
result = response.json()
# Extract and print the generated text
generated_text = result['choices'][0]['message']['content']
return generated_text
else:
return f"Error: {response.status_code}, {response.text}"
def english_text_to_image(prompt):
payload = {
"inputs": prompt,
}
response = requests.post(IMAGE_API_URL, headers=img_headers, json=payload)
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
return image
# Custom CSS
# st.markdown("""
# <style>
# /* Background color */
# body {
# background-color: #f0f0f5;
# }
# /* Text color and font */
# .stApp {
# font-family: 'Arial', sans-serif;
# color: #333;
# }
# /* Titles and subtitles styling */
# h1 {
# color: #2E8B57;
# text-align: center;
# text-shadow: 2px 2px 5px #aaaaaa;
# }
# h2, h3 {
# color: #4682B4;
# text-shadow: 1px 1px 3px #aaaaaa;
# }
# /* Background texture */
# .stApp {
# background: linear-gradient(to bottom right, #fff7e6, #e6f7ff);
# }
# /* Button styling */
# button[kind="primary"] {
# background-color: #4682B4;
# color: white;
# border-radius: 8px;
# padding: 0.5rem 1rem;
# }
# button[kind="primary"]:hover {
# background-color: #5b9bd5;
# }
# /* Text area and input field styling */
# textarea, input {
# border-radius: 10px;
# padding: 1rem;
# border: 2px solid #ccc;
# background-color: #f9f9f9;
# }
# /* Styling the output boxes */
# .stMarkdown {
# background-color: #e6f9ff;
# padding: 1rem;
# border-radius: 10px;
# box-shadow: 2px 2px 10px #ccc;
# }
# </style>
# """, unsafe_allow_html=True)
# #sidebar styling
# st.markdown("""
# <style>
# [data-testid=stSidebar] {
# background-color: #FFFFFF;
# margin-right: 20px;
# border-right: 2px solid #FFFFFF
# }
# </style>
# """, unsafe_allow_html=True)
#options styling in sidebar and added image in sidebar
with st.sidebar:
selected = option_menu(
menu_title="",
options=['Home','Tool'],
icons=['house-door-fill','setting'],
menu_icon='truck-front-fill',
default_index=0,
styles={
"container": {'padding':'5!important','background-color':'#FAF9F6'},
"icon": {'color':"#000000", "font-size":"23px"},
"nav-link": {'font-size':'16px','text-align':'left','margin':'0px','--hover-color':'#EDEADE','font-weight':'bold'},
"nav-link-selector":{'background-color':'#E6E6FA','font-weight':'bold'}
}
)
if selected == "Home":
# Page title and header
st.title(":blue[Multi-Purpose Tool] - Empowering Educators πŸŽ“")
# Subheader for the app description
st.subheader("A versatile tool designed to assist teachers in translating, summarizing, and visualizing concepts.")
# Main description with detailed information about the app
st.markdown("""
The **Multi-Purpose Tool** is a user-friendly platform developed for educators,
enabling them to enhance their teaching experience. Whether it's translating content
into different languages, summarizing lengthy materials, or visualizing concepts
through images, this tool provides a one-stop solution for modern teaching needs.
### Key Features:
- **Translation**: Translate text seamlessly between languages (e.g., Tamil to English).
- **Summarization**: Quickly generate summaries of long passages for easy understanding.
- **Text to Image**: Visualize difficult concepts by generating images from text descriptions.
### Available Worldwide:
The Multi-Purpose Tool is deployed on Hugging Face and accessible globally to teachers
and educators at the click of a button. Visit the [app here](https://huggingface.co/spaces/Jesivn/Multi_purpose_Software).
Empower your classroom with advanced AI tools today!
""")
elif selected=="Tool":
# Row 1: Tamil to English translation
st.subheader("🌐 Translate Tamil to English")
tamil_input = st.text_area("Enter Tamil text", "")
if st.button("Translate"):
english_output = translate_tamil_to_english(tamil_input)
st.markdown(f"**Translated English Text**: {english_output}")
# Row 2: English paragraph summarization
st.subheader("πŸ“ Summarize English Paragraph")
english_paragraph = st.text_area("Enter English paragraph", "")
if st.button("Summarize"):
summary_output = summarize_english_text(english_paragraph)
st.markdown(f"**Summary**: {summary_output}")
# Row 3: English text to image generation
st.subheader("🎨 Generate Image from English Text")
image_text = st.text_input("Enter description for image generation", "")
if st.button("Generate Image"):
generated_image = english_text_to_image(image_text)
st.image(generated_image, caption="Generated Image")