Spaces:
Runtime error
Runtime error
File size: 2,374 Bytes
7b0125b 5b1b789 faf6bcf 77c7e78 9ee147b bf82696 1bd4be7 bf82696 1ce9c25 bf82696 faf6bcf 1ce9c25 bf82696 faf6bcf 30d0996 faf6bcf bf82696 faf6bcf f698758 30d0996 faf6bcf 30d0996 d328400 faf6bcf 30d0996 bf82696 faf6bcf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import gradio as gr
import shap
from transformers import pipeline
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
import streamlit as st
import sys
import os
import pandas as pd
import json
from confluent_kafka import Consumer
from ast import literal_eval
consumer = Consumer(
{'bootstrap.servers': 'pkc-41973.westus2.azure.confluent.cloud:9092',
"group.id": "group_data_h",
'security.protocol':'SASL_SSL',
'sasl.mechanisms':'PLAIN',
'sasl.username':'AIZHFU6TZHAQC5E3',
'sasl.password':os.environ.get("confluent_ingreso"),
'auto.offset.reset': 'earliest',
'enable.auto.commit': True })
consumer.subscribe(['factored_datathon_amazon_review_1'])
i=0
received=[]
df = {}
model_name = "sohan-ai/sentiment-analysis-model-amazon-reviews"
tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased")
model = DistilBertForSequenceClassification.from_pretrained(model_name)
input_text="Awaiting Reviews"
def interpretation_function(text):
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
predicted_label = "positive" if outputs.logits.argmax().item() == 1 else "negative"
return {"Review": text, "interpretation": predicted_label}
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="Sentiment Analysis", value=input_text)
with gr.Row():
interpret = gr.Button("Interpret Review")
with gr.Column():
interpretation = gr.components.Interpretation(input_text)
demo.load(interpretation_function, input_text, interpretation,every=60)
interpret.click(interpretation_function, input_text, interpretation)
demo.queue(api_open=False)
try:
while True:
msg = consumer.poll(1.0)
if msg is None:
continue
user = msg.value()
if user is not None:
nus=literal_eval(user.decode('utf8'))
dato=json.loads(json.dumps(nus, indent=4))
df[i] = dato
df_t=pd.DataFrame.from_dict(df, orient='index')
input_text = df_t.iloc[[i],[2]]
i += 1
except SystemExit:
print('closing the consumer')
consumer.close()
if __name__ == "__main__":
demo.launch() |