Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizerimport gradio as grimport torchfrom concurrent.futures import ThreadPoolExecutorfrom threading import Lock# Global cache and lock for thread-safetyCACHE_SIZE = 100prediction_cache = {}cache_lock = Lock()# Mapping for sentiment labels from cardiffnlp/twitter-roberta-base-sentimentSENTIMENT_LABEL_MAPPING = {"LABEL_0": "negative","LABEL_1": "neutral","LABEL_2": "positive"}def load_model(model_name):"""Loads the model with 8-bit quantization if a GPU is available;otherwise, loads the full model."""if torch.cuda.is_available():model = AutoModelForSequenceClassification.from_pretrained(model_name, load_in_8bit=True, device_map="auto")tokenizer = AutoTokenizer.from_pretrained(model_name)device = 0 # GPU indexelse:model = AutoModelForSequenceClassification.from_pretrained(model_name)tokenizer = AutoTokenizer.from_pretrained(model_name)device = -1return pipeline("text-classification", model=model, tokenizer=tokenizer, device=device)# Load both models concurrently at startup.with ThreadPoolExecutor() as executor:sentiment_future = executor.submit(load_model, "cardiffnlp/twitter-roberta-base-sentiment")emotion_future = executor.submit(load_model, "bhadresh-savani/bert-base-uncased-emotion")sentiment_pipeline = sentiment_future.result()emotion_pipeline = emotion_future.result()def analyze_text(text):# Check cache first (thread-safe)with cache_lock:if text in prediction_cache:return prediction_cache[text]try:# Run both model inferences in parallel.with ThreadPoolExecutor() as executor:future_sentiment = executor.submit(sentiment_pipeline, text)future_emotion = executor.submit(emotion_pipeline, text)sentiment_result = future_sentiment.result()[0]emotion_result = future_emotion.result()[0]# Remap the sentiment label to a human-readable format if available.raw_sentiment_label = sentiment_result.get("label", "")sentiment_label = SENTIMENT_LABEL_MAPPING.get(raw_sentiment_label, raw_sentiment_label)# Format the output with rounded scores.result = {"Sentiment": {sentiment_label: round(sentiment_result['score'], 4)},"Emotion": {emotion_result['label']: round(emotion_result['score'], 4)}}except Exception as e:result = {"error": str(e)}# Update the cache in a thread-safe manner.with cache_lock:if len(prediction_cache) >= CACHE_SIZE:prediction_cache.pop(next(iter(prediction_cache)))prediction_cache[text] = resultreturn result# Define the Gradio interface.demo = gr.Interface(fn=analyze_text,inputs=gr.Textbox(placeholder="Enter your text here...", label="Input Text"),outputs=gr.JSON(label="Analysis Results"),title="🚀 Fast Sentiment & Emotion Analysis",description="Optimized application that remaps sentiment labels and uses parallel processing.",examples=[["I'm thrilled to start this new adventure!"],["This situation is making me really frustrated."],["I feel so heartbroken and lost."]],theme="soft",allow_flagging="never")# Warm up the models with a sample input._ = analyze_text("Warming up models...")if __name__ == "__main__":# Bind to all interfaces for Hugging Face Spaces.demo.launch(server_name="0.0.0.0", server_port=7860)1234567891011121314151617181920212223242526272829303132333435363738from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessorimport gradio as grfrom PIL import Image# Load the pre-trained Pix2Struct model and processormodel_name = "google/pix2struct-mathqa-base"model = Pix2StructForConditionalGeneration.from_pretrained(model_name)processor = Pix2StructProcessor.from_pretrained(model_name)# Function to solve handwritten math problemsdef solve_math_problem(image):# Preprocess the imageinputs = processor(images=image, text="Solve the math problem:", return_tensors="pt")# Generate the solutionpredictions = model.generate(**inputs, max_new_tokens=100)# Decode the outputsolution = processor.decode(predictions[0], skip_special_tokens=True)return solution# Gradio interfacedemo = gr.Interface(fn=solve_math_problem,inputs=gr.Image(type="pil", label="Upload Handwritten Math Problem"),outputs=gr.Textbox(label="Solution"),title="Handwritten Math Problem Solver",description="Upload an image of a handwritten math problem, and the model will solve it.",examples=[["example1.jpg"], # Add example images["example2.jpg"]],theme="soft")# Launch the appif __name__ == "__main__":demo.launch()
app.py+26-87