Fake-news-facts / app.py
pyresearch's picture
Upload app.py
4e838e8 verified
raw
history blame
9.87 kB
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import streamlit as st
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
# GPT-4 credentials
PAT_GPT4 = "3ca5bd8b0f2244eb8d0e4b2838fc3cf1"
USER_ID_GPT4 = "openai"
APP_ID_GPT4 = "chat-completion"
MODEL_ID_GPT4 = "openai-gpt-4-vision"
MODEL_VERSION_ID_GPT4 = "266df29bc09843e0aee9b7bf723c03c2"
# DALL-E credentials
PAT_DALLE = "bfdeb4029ef54d23a2e608b0aa4c00e4"
USER_ID_DALLE = "openai"
APP_ID_DALLE = "dall-e"
MODEL_ID_DALLE = "dall-e-3"
MODEL_VERSION_ID_DALLE = "dc9dcb6ee67543cebc0b9a025861b868"
# TTS credentials
PAT_TTS = "bfdeb4029ef54d23a2e608b0aa4c00e4"
USER_ID_TTS = "openai"
APP_ID_TTS = "tts"
MODEL_ID_TTS = "openai-tts-1"
MODEL_VERSION_ID_TTS = "fff6ce1fd487457da95b79241ac6f02d"
# NewsGuardian model credentials
PAT_NEWSGUARDIAN = "your_news_guardian_pat"
USER_ID_NEWSGUARDIAN = "your_user_id"
APP_ID_NEWSGUARDIAN = "your_app_id"
MODEL_ID_NEWSGUARDIAN = "your_model_id"
MODEL_VERSION_ID_NEWSGUARDIAN = "your_model_version_id"
# Set up gRPC channel for NewsGuardian model
channel_tts = ClarifaiChannel.get_grpc_channel()
stub_tts = service_pb2_grpc.V2Stub(channel_tts)
metadata_tts = (('authorization', 'Key ' + PAT_TTS),)
userDataObject_tts = resources_pb2.UserAppIDSet(user_id=USER_ID_TTS, app_id=APP_ID_TTS)
# Streamlit app
st.title("NewsGuardian")
# Inserting logo
st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTdA-MJ_SUCRgLs1prqudpMdaX4x-x10Zqlwp7cpzXWCMM9xjBAJYWdJsDlLoHBqNpj8qs&usqp=CAU")
# Function to generate text using the "microsoft/phi-2" model
def generate_phi2_text(input_text):
inputs = tokenizer(input_text, return_tensors="pt", return_attention_mask=False)
outputs = model.generate(**inputs, max_length=200)
generated_text = tokenizer.batch_decode(outputs)[0]
return generated_text
# User input
raw_text_phi2 = st.text_area("Enter text for phi-2 model")
# Button to generate result using "microsoft/phi-2" model
if st.button("NewsGuardian model Generated fake news with phi-2"):
if raw_text_phi2:
generated_text_phi2 = generate_phi2_text(raw_text_phi2)
st.text("NewsGuardian model Generated fake news with phi-2")
st.text(generated_text_phi2)
else:
st.warning("Please enter news phi-2 model")
# User input
model_type = st.selectbox("Select Model", ["NewsGuardian model", "DALL-E"])
raw_text_news_guardian = st.text_area("This news is real or fake?")
image_upload_news_guardian = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
# Button to generate result for NewsGuardian model
if st.button("NewsGuardian News Result"):
if model_type == "NewsGuardian model":
# Set up gRPC channel for NewsGuardian model
channel_news_guardian = ClarifaiChannel.get_grpc_channel()
stub_news_guardian = service_pb2_grpc.V2Stub(channel_news_guardian)
metadata_news_guardian = (('authorization', 'Key ' + PAT_NEWSGUARDIAN),)
userDataObject_news_guardian = resources_pb2.UserAppIDSet(user_id=USER_ID_NEWSGUARDIAN, app_id=APP_ID_NEWSGUARDIAN)
# Prepare the request for NewsGuardian model
input_data_news_guardian = resources_pb2.Data()
if raw_text_news_guardian:
input_data_news_guardian.text.raw = raw_text_news_guardian
if image_upload_news_guardian is not None:
image_bytes_news_guardian = image_upload_news_guardian.read()
input_data_news_guardian.image.base64 = image_bytes_news_guardian
post_model_outputs_response_news_guardian = stub_news_guardian.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
user_app_id=userDataObject_news_guardian,
model_id=MODEL_ID_NEWSGUARDIAN,
version_id=MODEL_VERSION_ID_NEWSGUARDIAN,
inputs=[resources_pb2.Input(data=input_data_news_guardian)]
),
metadata=metadata_news_guardian # Use metadata directly in the gRPC request
)
# Check if the request was successful for NewsGuardian model
if post_model_outputs_response_news_guardian.status.code != status_code_pb2.SUCCESS:
st.error(f"NewsGuardian model API request failed: {post_model_outputs_response_news_guardian.status.description}")
else:
# Get the output for NewsGuardian model
output_news_guardian = post_model_outputs_response_news_guardian.outputs[0].data
# Display the result for NewsGuardian model
if output_news_guardian.HasField("image"):
st.image(output_news_guardian.image.base64, caption='Generated Image (NewsGuardian model)', use_column_width=True)
elif output_news_guardian.HasField("text"):
# Display the text result
st.text(output_news_guardian.text.raw)
# Convert text to speech and play the audio
tts_input_data = resources_pb2.Data()
tts_input_data.text.raw = output_news_guardian.text.raw
tts_response = stub_tts.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
user_app_id=userDataObject_tts,
model_id=MODEL_ID_TTS,
version_id=MODEL_VERSION_ID_TTS,
inputs=[resources_pb2.Input(data=tts_input_data)]
),
metadata=metadata_tts # Use the same metadata for TTS
)
# Check if the TTS request was successful
if tts_response.status.code == status_code_pb2.SUCCESS:
tts_output = tts_response.outputs[0].data
st.audio(tts_output.audio.base64, format='audio/wav')
else:
st.error(f"TTS API request failed: {tts_response.status.description}")
elif model_type == "DALL-E":
# Set up gRPC channel for DALL-E
channel_dalle = ClarifaiChannel.get_grpc_channel()
stub_dalle = service_pb2_grpc.V2Stub(channel_dalle)
metadata_dalle = (('authorization', 'Key ' + PAT_DALLE),)
userDataObject_dalle = resources_pb2.UserAppIDSet(user_id=USER_ID_DALLE, app_id=APP_ID_DALLE)
# Prepare the request for DALL-E
input_data_dalle = resources_pb2.Data()
if raw_text_news_guardian:
input_data_dalle.text.raw = raw_text_news_guardian
post_model_outputs_response_dalle = stub_dalle.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
user_app_id=userDataObject_dalle,
model_id=MODEL_ID_DALLE,
version_id=MODEL_VERSION_ID_DALLE,
inputs=[resources_pb2.Input(data=input_data_dalle)]
),
metadata=metadata_dalle
)
# Check if the request was successful for DALL-E
if post_model_outputs_response_dalle.status.code != status_code_pb2.SUCCESS:
st.error(f"DALL-E API request failed: {post_model_outputs_response_dalle.status.description}")
else:
# Get the output for DALL-E
output_dalle = post_model_outputs_response_dalle.outputs[0].data
# Display the result for DALL-E
if output_dalle.HasField("image"):
st.image(output_dalle.image.base64, caption='Generated Image (DALL-E)', use_column_width=True)
elif output_dalle.HasField("text"):
st.text(output_dalle.text.raw)
# Add the beautiful social media icon section
st.markdown("""
<div align="center">
<a href="https://github.com/pyresearch/pyresearch" style="text-decoration:none;">
<img src="https://user-images.githubusercontent.com/34125851/226594737-c21e2dda-9cc6-42ef-b4e7-a685fea4a21d.png" width="2%" alt="" /></a>
<img src="https://user-images.githubusercontent.com/34125851/226595799-160b0da3-c9e0-4562-8544-5f20460f7cc9.png" width="2%" alt="" />
<a href="https://www.linkedin.com/company/pyresearch/" style="text-decoration:none;">
<img src="https://user-images.githubusercontent.com/34125851/226596446-746ffdd0-a47e-4452-84e3-bf11ec2aa26a.png" width="2%" alt="" /></a>
<img src="https://user-images.githubusercontent.com/34125851/226595799-160b0da3-c9e0-4562-8544-5f20460f7cc9.png" width="2%" alt="" />
<a href="https://twitter.com/Noorkhokhar10" style="text-decoration:none;">
<img src="https://user-images.githubusercontent.com/34125851/226599162-9b11194e-4998-440a-ba94-c8a5e1cdc676.png" width="2%" alt="" /></a>
<img src="https://user-images.githubusercontent.com/34125851/226595799-160b0da3-c9e0-4562-8544-5f20460f7cc9.png" width="2%" alt="" />
<a href="https://www.youtube.com/@Pyresearch" style="text-decoration:none;">
<img src="https://user-images.githubusercontent.com/34125851/226599904-7d5cc5c0-89d2-4d1e-891e-19bee1951744.png" width="2%" alt="" /></a>
<img src="https://user-images.githubusercontent.com/34125851/226595799-160b0da3-c9e0-4562-8544-5f20460f7cc9.png" width="2%" alt="" />
<a href="https://www.facebook.com/Pyresearch" style="text-decoration:none;">
<img src="https://user-images.githubusercontent.com/34125851/226600380-a87a9142-e8e0-4ec9-bf2c-dd6e9da2f05a.png" width="2%" alt="" /></a>
<img src="https://user-images.githubusercontent.com/34125851/226595799-160b0da3-c9e0-4562-8544-5f20460f7cc9.png" width="2%" alt="" />
<a href="https://www.instagram.com/pyresearch/" style="text-decoration:none;">
<img src="https://user-images.githubusercontent.com/34125851/226601355-ffe0b597-9840-4e10-bbef-43d6c74b5a9e.png" width="2%" alt="" /></a>
</div>
<hr>
""", unsafe_allow_html=True)