Frenchizer commited on
Commit
4ebabb8
·
verified ·
1 Parent(s): 1957c1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -39
app.py CHANGED
@@ -23,26 +23,14 @@ labels = [
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
- "sophisticated", "playful", "serious", "friendly"
38
- ]
39
-
40
- styles = [
41
- "poetry", "novel", "theater", "slang", "speech", "keywords", "html", "programming"
42
- ]
43
-
44
- gender_number = [
45
- "masculine singular", "masculine plural", "feminine singular", "feminine plural"
46
  ]
47
 
48
  @lru_cache(maxsize=1)
@@ -60,7 +48,7 @@ def softmax(x):
60
  return exp_x / exp_x.sum()
61
 
62
  # Function to detect context
63
- def detect_context(input_text, threshold=0.03):
64
  # Encode the input text
65
  inputs = tokenizer([input_text], padding=True, truncation=True, return_tensors="pt")
66
  with torch.no_grad():
@@ -81,35 +69,48 @@ def detect_context(input_text, threshold=0.03):
81
 
82
  # If no contexts meet the threshold, default to "general"
83
  if not high_confidence_contexts:
84
- high_confidence_contexts = [("general", 1.0)]
85
 
86
  return high_confidence_contexts
87
 
88
- # Translation client
89
- translation_client = Client("Frenchizer/space_7")
90
-
91
- def translate_text(input_text):
92
- # Translate the input text
93
- return translation_client.predict(input_text)
 
 
 
 
 
 
 
 
94
 
95
  def process_request(input_text):
96
- # Step 1: Translate the text
97
- translation = translate_text(input_text)
98
-
99
- # Step 2: Detect context
100
  context_results = detect_context(input_text)
101
 
102
- # Step 3: Print the list of high-confidence contexts
103
- print("Contexts:", context_results)
 
 
 
 
 
 
104
 
105
- # Return the translation and contexts
106
- return translation, context_results
107
 
108
  # Gradio interface
109
  def gradio_interface(input_text):
110
- translation, contexts = process_request(input_text)
111
  # Format the output
112
- output = f"Translation: {translation}\n"
 
 
113
  return output.strip()
114
 
115
  # Create the Gradio interface
@@ -118,7 +119,7 @@ interface = gr.Interface(
118
  inputs="text",
119
  outputs="text",
120
  title="Frenchizer",
121
- description="Translate text from English to French with context detection and threshold."
122
  )
123
 
124
  interface.launch()
 
23
  "automotive", "blockchain", "biology", "chemistry",
24
  "cryptocurrency", "data science", "design", "e-commerce",
25
  "education", "engineering", "entertainment", "environment",
26
+ "fashion", "finance", "food commerce", "general",
27
+ "gaming", "healthcare", "history", "html",
28
+ "information technology", "IT", "keywords", "legal",
29
+ "literature", "machine learning", "marketing", "medicine",
30
+ "music", "personal development", "philosophy", "physics",
31
+ "politics", "poetry", "programming", "real estate", "retail",
32
+ "robotics", "slang", "social media", "speech", "sports",
33
+ "sustained", "technical", "theater", "tourism", "travel"
 
 
 
 
 
 
 
 
 
 
 
 
34
  ]
35
 
36
  @lru_cache(maxsize=1)
 
48
  return exp_x / exp_x.sum()
49
 
50
  # Function to detect context
51
+ def detect_context(input_text, threshold=0.022):
52
  # Encode the input text
53
  inputs = tokenizer([input_text], padding=True, truncation=True, return_tensors="pt")
54
  with torch.no_grad():
 
69
 
70
  # If no contexts meet the threshold, default to "general"
71
  if not high_confidence_contexts:
72
+ high_confidence_contexts = [("general", 1.0)] # Assign a default score of 1.0 for "general"
73
 
74
  return high_confidence_contexts
75
 
76
+ # Mock translation clients for different contexts
77
+ def get_translation_client(context):
78
+ """
79
+ Returns the appropriate Hugging Face Space client for the given context.
80
+ For now, all contexts use the same mock space.
81
+ """
82
+ return Client("Frenchizer/space_7") # Replace with actual Space paths for each context
83
+
84
+ def translate_text(input_text, context):
85
+ """
86
+ Translates the input text using the appropriate model for the given context.
87
+ """
88
+ client = get_translation_client(context)
89
+ return client.predict(input_text)
90
 
91
  def process_request(input_text):
92
+ # Step 1: Detect context
 
 
 
93
  context_results = detect_context(input_text)
94
 
95
+ # Step 2: Translate the text for each context
96
+ translations = {}
97
+ for context, score in context_results:
98
+ translations[context] = translate_text(input_text, context)
99
+
100
+ # Step 3: Print the list of high-confidence contexts and translations
101
+ print("High-confidence contexts (score >= 0.022):", context_results)
102
+ print("Translations:", translations)
103
 
104
+ # Return the translations and contexts
105
+ return translations, context_results
106
 
107
  # Gradio interface
108
  def gradio_interface(input_text):
109
+ translations, contexts = process_request(input_text)
110
  # Format the output
111
+ output = "Translations:\n\n"
112
+ for context, translation in translations.items():
113
+ output += f"**{context.capitalize()}**: {translation}\n\n"
114
  return output.strip()
115
 
116
  # Create the Gradio interface
 
119
  inputs="text",
120
  outputs="text",
121
  title="Frenchizer",
122
+ description="Translate text from English to French with context-specific models and different outputs."
123
  )
124
 
125
  interface.launch()