isriam commited on
Commit
923d5db
1 Parent(s): ffd95fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -25
app.py CHANGED
@@ -1,34 +1,21 @@
1
- import torch
2
- from transformers import AutoTokenizer, AutoModelForQuestionAnswering
3
  import gradio as gr
 
 
4
 
5
- def answer_question(question, context):
6
- # Load the model and tokenizer
7
- model_name = "Qwen/Qwen2-VL"
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForQuestionAnswering.from_pretrained(model_name)
10
-
11
- # Tokenize the input
12
- inputs = tokenizer(question, context, return_tensors="pt")
13
-
14
- # Generate the answer
15
  with torch.no_grad():
16
  outputs = model(**inputs)
17
-
18
- # Decode the answer
19
- answer = tokenizer.decode(outputs.start_logits.argmax(), skip_special_tokens=True)
20
-
21
- return answer
22
 
23
  interface = gr.Interface(
24
- fn=answer_question,
25
- inputs=[
26
- gr.inputs.Textbox(label="Question"),
27
- gr.inputs.Textbox(label="Context")
28
- ],
29
  outputs="text",
30
- title="Qwen2-VL Question Answering",
31
- description="Enter a question and context to get an answer.",
32
  )
33
-
34
  interface.launch()
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
 
5
+ def classify_text(text):
6
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
7
+ model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
8
+ inputs = tokenizer(text, return_tensors="pt")
 
 
 
 
 
 
9
  with torch.no_grad():
10
  outputs = model(**inputs)
11
+ predicted_class = torch.argmax(outputs.logits, dim=1).item()
12
+ return "Positive" if predicted_class == 1 else "Negative"
 
 
 
13
 
14
  interface = gr.Interface(
15
+ fn=classify_text,
16
+ inputs=gr.inputs.Textbox(label="Text"),
 
 
 
17
  outputs="text",
18
+ title="Sentiment Analysis",
19
+ description="Enter a text to classify its sentiment as positive or negative.",
20
  )
 
21
  interface.launch()