Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed ai issue #36

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ GITHUB_TOKEN=YOUR_GITHUB_TOKEN
# 2. Permissions: repo, user
# 3. Generate token
# 4. Copy token
# 5. Paste token in .env.local
# 5. Paste token in .env.local

API="api key" # your gemini api key
12 changes: 8 additions & 4 deletions ai/generateTagline.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
const { GoogleGenerativeAI } = require("@google/generative-ai");

const genAI = new GoogleGenerativeAI(process.env.API_enter_here);
const genAI = new GoogleGenerativeAI(process.env.API);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });

export async function generateUserTagline(username, contributions) {
const prompt = `
Generate a custom tagline for the GitHub user "${username}" based on the following activity and contribution patterns:
- Total Contributions: ${contributions.totalCommitContributions}
- Active Coding Days: ${contributions.contributionCalendar.totalContributions}
- Most Used Programming Languages: ${topLanguages}

The tagline should be consistent, meaningful, and provide an at-a-glance summary of the user's work.
The tagline should be consistent, meaningful, and provide an at-a-glance summary of the user's work..
Only generate one tagline, only one.
`;

try {
const result = await model.generateContent(prompt);
console.log("AI");

console.log(result.response.text());

if (result.response) {
return result.response.text;
return result.response.text();
} else {
throw new Error("Tagline not found in the response");
}
Expand Down
2 changes: 2 additions & 0 deletions components/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function Home() {
body: JSON.stringify({ username: username.trim() }),
});

console.log(response)

if (response.ok) {
const data = await response.json();
console.log(data);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"postcss": "^8",
"tailwindcss": "^3.4.1"
}
}
}
18 changes: 14 additions & 4 deletions src/app/[username]/page.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use client';
import { generateUserTagline } from "../../../ai/generateTagline";
import { useSearchParams, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import {contributions} from '../api/ai/route'
import axios from "axios";

function UserPage({ params }) {
const { username } = params;
Expand All @@ -19,6 +18,19 @@ function UserPage({ params }) {
if (colorParam) setTextColor(decodeURIComponent(colorParam));
}, [searchParams]);

const [tagline, setTagline] = useState("");

const fetchTagline = async()=>{
const res = await axios.post(`http://localhost:3000/api/ai`, {
username
})

setTagline(res.data.tagline);
}
useEffect(()=>{
fetchTagline()
},[])

const handleThemeChange = (e) => {
const newTheme = e.target.value;
setTheme(newTheme);
Expand All @@ -36,8 +48,6 @@ function UserPage({ params }) {
router.push(`/${username}${queryString}`);
};

const tagline=generateUserTagline(username,contributions);

return (
<div
className="flex flex-col justify-center items-center h-screen"
Expand Down
11 changes: 9 additions & 2 deletions src/app/api/ai/route.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import { generateUserTagline } from "../../../../ai/generateTagline";
const GITHUB_API_URL = "https://api.github.com/graphql";

export async function POST(req) {
Expand All @@ -21,7 +22,6 @@ export async function POST(req) {
repository {
name
}
totalCount
}
}
repositories(first: 100, orderBy: {field: STARGAZERS, direction: DESC}) {
Expand All @@ -36,6 +36,8 @@ export async function POST(req) {
`;

try {
console.log("hii");

const response = await fetch(GITHUB_API_URL, {
method: "POST",
headers: {
Expand All @@ -49,16 +51,21 @@ export async function POST(req) {
}),
});

// console.log(response)
console.log("hello");


if (!response.ok) {
return new NextResponse(await response.text(), {
status: response.status,
});
}

const data = await response.json();
console.log(data);
// console.log(data);
if (data.data.user) {
const contributions = data.data.user.contributionsCollection;
// console.log(contributions)
const tagline = await generateUserTagline(username, contributions);
return NextResponse.json({
exists: true,
Expand Down
Loading