Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,51 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
"""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
],
|
60 |
-
)
|
61 |
-
|
|
|
|
|
62 |
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
|
5 |
+
# Input language translators (to English)
|
6 |
+
input_translators = {
|
7 |
+
"Hindi": pipeline("translation_hi_to_en", model="Helsinki-NLP/opus-mt-hi-en"),
|
8 |
+
"French": pipeline("translation_fr_to_en", model="Helsinki-NLP/opus-mt-fr-en"),
|
9 |
+
"German": pipeline("translation_de_to_en", model="Helsinki-NLP/opus-mt-de-en"),
|
10 |
+
"English": None # No translation needed
|
11 |
+
}
|
12 |
+
|
13 |
+
# Summarization model
|
14 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
15 |
+
|
16 |
+
# Output translators (English → target)
|
17 |
+
output_translators = {
|
18 |
+
"None": None,
|
19 |
+
"Hindi": pipeline("translation_en_to_hi", model="Helsinki-NLP/opus-mt-en-hi"),
|
20 |
+
"French": pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr"),
|
21 |
+
"German": pipeline("translation_en_to_de", model="Helsinki-NLP/opus-mt-en-de"),
|
22 |
+
}
|
23 |
+
|
24 |
+
def summarize_multilang(text, input_lang, output_lang):
|
25 |
+
# Step 1: Translate to English (if needed)
|
26 |
+
if input_lang != "English":
|
27 |
+
translator = input_translators[input_lang]
|
28 |
+
text = translator(text)[0]['translation_text']
|
29 |
+
|
30 |
+
# Step 2: Summarize
|
31 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
32 |
+
|
33 |
+
# Step 3: Translate summary (if needed)
|
34 |
+
if output_lang != "None":
|
35 |
+
summary = output_translators[output_lang](summary)[0]['translation_text']
|
36 |
+
|
37 |
+
return summary
|
38 |
+
|
39 |
+
# Gradio interface
|
40 |
+
gr.Interface(
|
41 |
+
fn=summarize_multilang,
|
42 |
+
inputs=[
|
43 |
+
gr.Textbox(lines=10, label="Input Text"),
|
44 |
+
gr.Dropdown(choices=["English", "Hindi", "French", "German"], label="Input Language"),
|
45 |
+
gr.Dropdown(choices=["None", "Hindi", "French", "German"], label="Translate Summary To")
|
46 |
],
|
47 |
+
outputs=gr.Textbox(label="Final Summary"),
|
48 |
+
title="SummarAI",
|
49 |
+
description="Supports input in Hindi, French, German, or English. Summarizes and optionally translates the summary."
|
50 |
+
).launch()
|
51 |
|
|
|
|