Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""app.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1neCwOaMM4pn-eYYwZ6zYIdhhNdiwIG5K
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from openai import OpenAI
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
import os
|
| 15 |
+
os.environ["groq_api_key"] = "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"
|
| 16 |
+
|
| 17 |
+
# Initialize Groq client
|
| 18 |
+
client = OpenAI(
|
| 19 |
+
api_key=os.environ["groq_api_key"],
|
| 20 |
+
base_url="https://api.groq.com/openai/v1"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Function to send prompts to Groq's Llama 3 model
|
| 24 |
+
def lcpp_llm(prompt, max_tokens=512, temperature=0.3, stop=None):
|
| 25 |
+
response = client.chat.completions.create(
|
| 26 |
+
model="llama3-8b-8192",
|
| 27 |
+
messages=[{"role": "user", "content": prompt}],
|
| 28 |
+
max_tokens=max_tokens,
|
| 29 |
+
temperature=temperature,
|
| 30 |
+
stop=stop
|
| 31 |
+
)
|
| 32 |
+
return response.choices[0].message.content.strip()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def pos_function(user_query):
|
| 36 |
+
POS_SYSTEM_MESSAGE="""
|
| 37 |
+
SYSTEM:
|
| 38 |
+
You are a linguistics expert and NLP model trained to analyze the grammatical structure of English text.
|
| 39 |
+
Your task is to perform Part-of-Speech (POS) tagging for the following customer review.
|
| 40 |
+
For each word, return its POS tag in a clean, aligned format.
|
| 41 |
+
|
| 42 |
+
USER: """
|
| 43 |
+
prompt=POS_SYSTEM_MESSAGE+user_query
|
| 44 |
+
output = lcpp_llm(prompt, max_tokens=512)
|
| 45 |
+
return output
|
| 46 |
+
|
| 47 |
+
def ner_function(user_query):
|
| 48 |
+
NER_SYSTEM_MESSAGE="""
|
| 49 |
+
SYSTEM:
|
| 50 |
+
You are an Information Extraction Specialist AI that extracts meaningful entities from customer feedback.
|
| 51 |
+
Identify all named entities in the review and classify them into the appropriate types (e.g., PERSON, ORGANIZATION, PRODUCT, LOCATION, DATE, etc.).
|
| 52 |
+
Return your output in a structured table with two columns: Entity and Label. Do not ask any follow up questions.
|
| 53 |
+
|
| 54 |
+
USER:
|
| 55 |
+
"""
|
| 56 |
+
prompt=NER_SYSTEM_MESSAGE+user_query
|
| 57 |
+
output = lcpp_llm(prompt, max_tokens=512)
|
| 58 |
+
return output
|
| 59 |
+
|
| 60 |
+
def analysis_function(user_query):
|
| 61 |
+
ANALYSIS_SYSTEM_MESSAGE="""
|
| 62 |
+
SYSTEM:
|
| 63 |
+
You are a Customer Experience Analyst AI. Analyze the customer review below and create a concise table with three columns:
|
| 64 |
+
1. Category (Product or Service) mentioned in comment
|
| 65 |
+
2. Sentiment (Positive, Negative, or Mixed)
|
| 66 |
+
3. Insight (What is going well or needs improvement)
|
| 67 |
+
|
| 68 |
+
Only output the table Focus on clear business insights. Do not ask any follow up questions.
|
| 69 |
+
USER:
|
| 70 |
+
"""
|
| 71 |
+
prompt=ANALYSIS_SYSTEM_MESSAGE+user_query
|
| 72 |
+
output= lcpp_llm(prompt, max_tokens=512)
|
| 73 |
+
# Keep only the first table if repeated
|
| 74 |
+
if output.count("| Category | Sentiment | Insight |") > 1:
|
| 75 |
+
first_table = output.split("| Category | Sentiment | Insight |", 1)[1]
|
| 76 |
+
first_table = "| Category | Sentiment | Insight |\n" + first_table.split("SYSTEM:")[0].strip()
|
| 77 |
+
return first_table
|
| 78 |
+
else:
|
| 79 |
+
return output
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Gradio interface
|
| 83 |
+
def full_analysis(user_query):
|
| 84 |
+
pos = pos_function(user_query)
|
| 85 |
+
ner = ner_function(user_query)
|
| 86 |
+
analysis = analysis_function(user_query)
|
| 87 |
+
return pos, ner, analysis
|
| 88 |
+
|
| 89 |
+
iface = gr.Interface(
|
| 90 |
+
fn=full_analysis,
|
| 91 |
+
inputs=gr.Textbox(lines=5, label="Enter Customer Review"),
|
| 92 |
+
outputs=[
|
| 93 |
+
gr.Textbox(label="POS Tags"),
|
| 94 |
+
gr.Textbox(label="Named Entities"),
|
| 95 |
+
gr.Textbox(label="Review Sentiment & Insights")
|
| 96 |
+
],
|
| 97 |
+
title="Customer Review Analyzer with Llama 3 (POS + NER + Sentiment)",
|
| 98 |
+
description="This tool uses Meta-Llama-3-8B-Instruct (GGUF) to extract POS tags, named entities, and sentiment insights from customer reviews."
|
| 99 |
+
)
|
| 100 |
+
iface.launch()
|