Frenchizer commited on
Commit
f9220f6
·
verified ·
1 Parent(s): a74487c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -35
app.py CHANGED
@@ -1,37 +1,68 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModel
3
- from sklearn.metrics.pairwise import cosine_similarity
4
- import torch
5
- import numpy as np
6
- from gradio_client import Client
7
- from functools import lru_cache
8
-
9
- # Cache the model and tokenizer using lru_cache
10
- @lru_cache(maxsize=1)
11
- def load_model_and_tokenizer():
12
- model_name = "./all-MiniLM-L6-v2" # Replace with your Space and model path
13
- tokenizer = AutoTokenizer.from_pretrained(model_name)
14
- model = AutoModel.from_pretrained(model_name)
15
- return tokenizer, model
16
-
17
- # Load the model and tokenizer
18
- tokenizer, model = load_model_and_tokenizer()
19
-
20
- # Precompute label embeddings
21
- labels = [
22
- "aerospace", "anatomy", "anthropology", "art",
23
- "automotive", "blockchain", "biology", "chemistry",
24
- "cryptocurrency", "data science", "design", "e-commerce",
25
- "education", "engineering", "entertainment", "environment",
26
- "fashion", "finance", "food commerce", "gaming",
27
- "healthcare", "history", "information technology",
28
- "legal", "machine learning", "marketing", "medicine",
29
- "music", "philosophy", "physics", "politics", "real estate", "retail",
30
- "robotics", "social media", "sports", "technical",
31
- "tourism", "travel"
32
- ]
33
-
34
- tones = [
35
- "formal", "positive", "negative", "poetic", "polite", "subtle", "casual", "neutral",
36
- "informal", "pompous", "sustained", "rude", "sustained",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  interface.launch()
 
1
  import gradio as gr
2
+
3
+ # Compute cosine similarities
4
+ similarities = cosine_similarity(input_embedding, label_embeddings)[0]
5
+
6
+ # Apply softmax to convert similarities to probabilities
7
+ probabilities = softmax(similarities)
8
+
9
+ # Pair each label with its probability
10
+ label_probabilities = list(zip(labels, probabilities))
11
+
12
+ # Filter contexts with confidence >= threshold
13
+ high_confidence_contexts = [(label, score) for label, score in label_probabilities if score >= threshold]
14
+
15
+ # If no contexts meet the threshold, default to "general"
16
+ if not high_confidence_contexts:
17
+ high_confidence_contexts = [("general", 1.0)] # Assign a default score of 1.0 for "general"
18
+
19
+ return high_confidence_contexts
20
+
21
+ # Mock translation clients for different contexts
22
+ def get_translation_client(context):
23
+ """
24
+ Returns the appropriate Hugging Face Space client for the given context.
25
+ For now, all contexts use the same mock space.
26
+ """
27
+ return Client("Frenchizer/space_18") # Replace with actual Space paths for each context
28
+
29
+ def translate_text(input_text, context):
30
+ """
31
+ Translates the input text using the appropriate model for the given context.
32
+ """
33
+ client = get_translation_client(context)
34
+ return client.predict(input_text)
35
+
36
+ def process_request(input_text):
37
+ # Step 1: Detect context
38
+ context_results = detect_context(input_text)
39
+
40
+ # Step 2: Translate the text for each context
41
+ translations = {}
42
+ for context, score in context_results:
43
+ translations[context] = translate_text(input_text, context)
44
+
45
+ # Step 3: Print the list of high-confidence contexts and translations
46
+ print("High-confidence contexts (score >= 0.022):", context_results)
47
+ print("Translations:", translations)
48
+
49
+ # Return the translations and contexts
50
+ return translations, context_results
51
+
52
+ # Gradio interface
53
+ def gradio_interface(input_text):
54
+ translation, contexts = process_request(input_text)
55
+ # Format the output
56
+ output = f"{translation}\n"
57
+ return output.strip()
58
+
59
+ # Create the Gradio interface
60
+ interface = gr.Interface(
61
+ fn=gradio_interface,
62
+ inputs="text",
63
+ outputs="text",
64
+ title="Frenchizer",
65
+ description="Translate text from English to French with context detection and threshold."
66
+ )
67
+
68
  interface.launch()